3

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?

Mwanji Ezana
  • 924
  • 8
  • 15

2 Answers2

6

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.

Community
  • 1
  • 1
Joel
  • 29,538
  • 35
  • 110
  • 138
2

Yes, with usage of entity manager and passing a criteria query as parameter:

List<?> results = em.createQuery(criteria).setFirstResult(offset).setMaxResults(5).getResultList();
Oleksii Kyslytsyn
  • 2,458
  • 2
  • 27
  • 43