Is there a way within a construction of a single criteria object (and therefore a single sql statement) that not only gets a page of data with a firstResult, maxResults setting, but also a Projection.rowCount()?
I was on Matt Riable's wiki and saw his post:
http://raibledesigns.com/wiki/Wiki.jsp?page=HibernatePagination
He has code that looks like this:
Code:
Query q = s.createFilter( collection, "" ); // the trivial filter
q.setMaxResults(PAGE_SIZE);
q.setFirstResult(PAGE_SIZE * pageNumber);
q.setProjection( Projections.alias( Projections.rowCount(), "numResults" ) )
List page = q.list();
However, I cannot get that to work on Criteria objects. The returning resultset is always empty. When I remove the last projection line, I always get results so I know the criteria is good. I thought perhaps that "numResults" was a reserved keyword since there is no context as to what it is. But I guess it is not.
(tried this)
Code:
Criteria c = ...
c.setMaxResults(PAGE_SIZE);
c.setFirstResult(PAGE_SIZE * pageNumber);
c.setProjection( Projections.alias( Projections.rowCount(), "numResults" ) )
List page = c.list();
I have posted a question on the Oracle Forums to see if this is even possible and I received very favorable opinions which lead me to believe that from a sql perspective it is possible.
http://forums.oracle.com/forums/thread.jspa?threadID=510229
So, does anyone know how to do this in a Hibernate criteria object? The reason why I want to know is that the cost of the query is rather expensive (triple join) and we want to minimize a performance hit with two sql statements for the same info - one to get the page of data and one to get the total available.
Anyone know how to do this?
Thanks in advance,
Mark