persistence@glassfish.java.net

Query Pagination, setFirstResult, setMaxResults looking for getTotalResults

From: Farrukh Najmi <farrukh_at_wellfleetsoftware.com>
Date: Mon, 31 Aug 2009 15:34:35 -0400

Hi Guys,

I am trying to use JPA pagination using code such as:

Query q = ...;
q.setFirstResult(20);
q.setMaxResults(10);
List results = q.getResultList(); //return results from the 20th position to 29th

To do this in a pagination loop I need to know how many objects in total
match the query if no pagination control where specified.
Is there a way to get this information without executing an extra query
without the pagination controls. I realize I could do a hopefully
inexpensive "SELECT COUNT(id)" query but that seems hard as I would have
to parse the original query and transform it into a SELECT COUNT equivalent.

So what do you suggest? Thanks.

-- 
Regards,
Farrukh
Web: http://www.wellfleetsoftware.com