How can we calculate a LAST page with JPA?

I would like to implement pagination in my Servlet / EJB / JPA-Hibernate project, but I cannot figure out how only one page is from the request and know the number of pages that I have to display

I use

setFirstResult(int first) ;
setMaxResults(int max) ;

and this works fine, but how can I find out how many pages I will have in total?

(Hibernate is my JPA provider, but I would prefer to use only JPA if possible)

UPDATE: COUNT () seems like the best / easiest solution; but what can be the cost SELECT COUNT(*) FROM ...compared to executeQuery("SELECT * FROM ...).getListResult().size()?

+5
source share
1 answer

AFAIK, (1) , (2) . - / .

, - COUNT(*),

Query query=em.createQuery("SELECT COUNT
(emp.empName) FROM Employee emp");

,

criteria.setProjection(Projections.rowCount());
int rowCount = (Integer) criteria.list().get(0);

, , , - .

:

,

+4

All Articles