JPA 2 CriteriaQuery,使用限制 [英] JPA 2 CriteriaQuery, using a limit
问题描述
我正在使用JPA 2.出于安全考虑,我正在使用CriteriaQuery进行类型安全(因此,我不会搜索任何键入查询的解决方案等等)。
I am using JPA 2. For safety reasons, I am working type safe with CriteriaQuery's (and thus, I am not searching for any solutions to typed queries and so on).
我最近遇到了一个需要设置SQL-LIMIT的问题。
I recently came across an issue in which I needed to set a SQL-LIMIT.
经过大量搜索后,我仍未成功找到解决方案。
After a lot of searching, I was still not successful in finding a solution.
CriteriaQuery<Product> query = getEntityManager().getCriteriaBuilder().createQuery(Product.class);
Root<Product> product = query.from(Product.class);
query.select(product);
return em.createQuery(query).getResultList();
任何人都可以帮助我吗?
Can anyone help me?
推荐答案
return em.createQuery(query)
.setFirstResult(offset) // offset
.setMaxResults(limit) // limit
.getResultList();
来自文档:
TypedQuery setFirstResult(int startPosition)
TypedQuery setFirstResult(int startPosition)
设置要检索的第一个结果的位置。参数:
startPosition - 第一个结果的位置,编号为0
Set the position of the first result to retrieve. Parameters: startPosition - position of the first result, numbered from 0
TypedQuery setMaxResults(int maxResult)
设置要检索的最大结果数。
Set the maximum number of results to retrieve.
这篇关于JPA 2 CriteriaQuery,使用限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!