Limit number of results in JPQL

橙三吉。 提交于 2019-12-17 07:26:40

问题


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 */

回答1:


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.




回答2:


You can set an offset too using setFirstResult()

    em.createNamedQuery("Entity.list")
      .setFirstResult(startPosition)
      .setMaxResults(length);


来源:https://stackoverflow.com/questions/3479128/limit-number-of-results-in-jpql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!