jpa 2 hibernate limit (max results) to a CriteriaQuery

元气小坏坏 提交于 2019-11-27 23:43:07

问题


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


回答1:


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.




回答2:


You could define the offset/limit like this:

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



回答3:


I usually use:

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


来源:https://stackoverflow.com/questions/3679131/jpa-2-hibernate-limit-max-results-to-a-criteriaquery

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