Reputation: 924
Is it possible to paginate a JPA 2 criteria query, as you can with in Hibernate with setFirstResult and setMaxResults?
If not, are there any workarounds?
Upvotes: 3
Views: 4980
Reputation: 2426
Yes, with usage of entity manager and passing a criteria query as parameter:
List<?> results = em.createQuery(criteria).setFirstResult(offset).setMaxResults(5).getResultList();
Upvotes: 2
Reputation: 30146
Duplicate of this question "jpa 2 hibernate limit (max results) to a CriteriaQuery"
A CriteriaQuery is not an executable Query. You need to create a TypedQuery first using EntityManager.createQuery(criteriaQuery). You can then set the max results of this and execute it.
Upvotes: 6