java jpa 2 休眠限制(最大结果)到 CriteriaQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3679131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:59:08  来源:igfitidea点击:

jpa 2 hibernate limit (max results) to a CriteriaQuery

javahibernatejpa-2.0criteriaquery

提问by Hugo

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?

也许这是一个愚蠢的问题,但我在文档中找不到答案:How can set a limit to the CriteriaQuery using JPA2?

Thanks

谢谢

回答by Mike Q

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.

CriteriaQuery 不是可执行的 Query。您需要先使用EntityManager.createQuery(criteriaQuery). 然后您可以设置它的最大结果并执行它。

回答by Uttam

You could define the offset/limit like this:

您可以像这样定义偏移/限制:

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

回答by Oleksii Kyslytsyn

I usually use:

我通常使用:

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