Limit the number of results in JPQL

How can I limit the number of results retrieved from the database?

select e from Entity e /* I need only 10 results for instance */ 
+47
java jpa jpql
Aug 13 '10 at 17:03
source share
2 answers

You can try like this to get 10 results that will be selected explicitly.

 entityManager.createQuery(JPQL_QUERY) .setParameter(arg0, arg1) .setMaxResults(10) .getResultList(); 

It will automatically create its own query in the background to get a certain amount of results if the backend supports it, and otherwise will make a memory limit after receiving all the results.

+58
Aug 16 2018-10-16
source share

You can also set the offset using setFirstResult ()

  em.createNamedQuery("Entity.list") .setFirstResult(startPosition) .setMaxResults(length); 
+19
Jun 11 '12 at 10:09
source share



All Articles