Limit number of results in JPQL

前端 未结 2 617
盖世英雄少女心
盖世英雄少女心 2020-11-29 07:17

How it is possible to limit the number of results retrieved from a database?

select e from Entity e /* I need only 10 results for instance */
相关标签:
2条回答
  • 2020-11-29 07:28

    You can try like this giving 10 results to be fetched explicitly.

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

    It will automatically create native query in back-end to retrieve specific number of results, if the backend supports it, and otherwise do the limit in memory after getting all results.

    0 讨论(0)
  • 2020-11-29 07:28

    You can set an offset too using setFirstResult()

    em.createNamedQuery("Entity.list")
      .setFirstResult(startPosition)
      .setMaxResults(length);
    
    0 讨论(0)
提交回复
热议问题