public class RollupResultHandler
extends java.lang.Object
implements org.apache.ibatis.session.ResultHandler
ResultHandler that collapses multiple rows based on a set of properties.
This class is derived from earlier RollupRowHandler used to workaround the groupBy and nested ResultMap
behaviour in iBatis (2.3.4.726) IBATIS-503.
The set of properties given act as a unique key. When the unique key changes, the collection
values from the nested ResultMap are coalesced and the given ResultHandler is called. It is
possible to embed several instances of this handler for deeply-nested ResultMap declarations.
Use this instance as a regular ResultHandler, but with one big exception: call RollupResultHandler.processLastResults()
after executing the SQL statement. Remove the groupBy attribute from the iBatis ResultMap
declaration.
Example iBatis 2.x (TODO migrate example to MyBatis 3.x):
Example usage:
<resultMap id="result_AuditQueryAllValues"
extends="alfresco.audit.result_AuditQueryNoValues"
class="AuditQueryResult">
<result property="auditValues" resultMap="alfresco.propval.result_PropertyIdSearchRow"/>
</resultMap>
RowHandler rowHandler = new RowHandler()
{
public void handleRow(Object valueObject)
{
// DO SOMETHING
}
};
RollupRowHandler rollupRowHandler = new RollupRowHandler(
new String[] {"auditEntryId"},
"auditValues",
rowHandler,
maxResults);
if (maxResults > 0)
{
// Calculate the maximum results required
int sqlMaxResults = (maxResults > 0 ? ((maxResults+1) * 20) : Integer.MAX_VALUE);
List
This class is not thread-safe; use a new instance for each use.
| Constructor and Description |
|---|
RollupResultHandler(java.lang.String[] keyProperties,
java.lang.String collectionProperty,
org.apache.ibatis.session.ResultHandler resultHandler) |
RollupResultHandler(java.lang.String[] keyProperties,
java.lang.String collectionProperty,
org.apache.ibatis.session.ResultHandler resultHandler,
int maxResults) |
| Modifier and Type | Method and Description |
|---|---|
void |
handleResult(org.apache.ibatis.session.ResultContext context) |
void |
processLastResults()
Client code must call this method once the query returns so that the final results
can be passed to the inner
RowHandler. |
public RollupResultHandler(java.lang.String[] keyProperties,
java.lang.String collectionProperty,
org.apache.ibatis.session.ResultHandler resultHandler)
keyProperties - the properties that make up the unique keycollectionProperty - the property mapped using a nested ResultMapresultHandler - the result handler that will receive the rolled-up resultspublic RollupResultHandler(java.lang.String[] keyProperties,
java.lang.String collectionProperty,
org.apache.ibatis.session.ResultHandler resultHandler,
int maxResults)
keyProperties - the properties that make up the unique keycollectionProperty - the property mapped using a nested ResultMapresultHandler - the result handler that will receive the rolled-up resultsmaxResults - the maximum number of results to retrieve (-1 for no limit).
Make sure that the query result limit is large enough to produce this
at least this number of resultspublic void handleResult(org.apache.ibatis.session.ResultContext context)
handleResult in interface org.apache.ibatis.session.ResultHandlerpublic void processLastResults()
RowHandler. If a query is limited by size, then it is
possible that the unprocessed results represent an incomplete final object; in this case
it would be best to ignore the last results. If the query is complete (i.e. all results
are returned) then this method should be called.
If you want X results and each result is made up of N rows (on average), then set the query
limit to:
L = X * (N+1)
and don't call this method.
Copyright © 2005 - 2010 Alfresco Software, Inc. All Rights Reserved.