Mwanji Ezana
Mwanji Ezana

Reputation: 924

Paginating a JPA 2 criteria query

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

Answers (2)

Oleksii Kyslytsyn
Oleksii Kyslytsyn

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

Joel
Joel

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

Related Questions