[hibernate-dev] Pagination: An approach for retrieving results / page count in single query

Jonathan Gerrish jongerrish at google.com
Mon Apr 21 14:17:10 EDT 2008


[Already posted to hibernate-users, but no responses.]

Hi,

I have pagination working, but am doing so with two queries; one to get the
results page and a second one to get the total count.

I'd like to do this in a single query: Is it possible to build a criteria,
so my results set includes columns for each of the properties on my bean,
plus an additional column which has the row count (downside: this is
duplicated for each row in the page).

I figured I could build a projection like this to return all the beans
properties plus the row count.

private Projection getProjectionsForBeanAndRowCount() {
ClassMetadata classMetadata =
session().getSessionFactory().getClassMetadata(modelClass);

ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.rowCount());
String[] propertyNames = classMetadata.getPropertyNames();
for (String propertyName : propertyNames) {
projectionList.add(Projections.property(propertyName));
}
return projectionList;
}

..and then write a ResultsTransformer to process these results,
instantiating a class like:-

public class PageResult {
Object bean;
int rowCount;
}

..but it seems that by the time ResultsTransformer is called, you already
have the bean instantiated.

Is there a way I can jump in at a slightly lower level, like at a single row
of the result set, the level where the results bean's are built?

Any info much appreciated.

All the best, Jon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/hibernate-dev/attachments/20080421/6fe5d0cf/attachment.html 


More information about the hibernate-dev mailing list