13

maybe it's a silly question but I cannot find the answer in the docs: How can set a limit to the CriteriaQuery using JPA2?

Thanks

3 Answers 3

21

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.

0
12

You could define the offset/limit like this:

return em.createQuery(query)
     .setFirstResult(offset) // offset
     .setMaxResults(limit) // limit
     .getResultList();
0

I usually use:

em.createQuery(criteria).setFirstResult(offset).setMaxResults(max).getResultList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.