JPA 2 CriteriaQuery, using a limit

Define limit and offset on the Query:

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

From the documentation:

TypedQuery setFirstResult(int startPosition)

Set the position of the first result to retrieve. Parameters:
startPosition – position of the first result, numbered from 0

TypedQuery setMaxResults(int maxResult)

Set the maximum number of results to retrieve.

Leave a Comment