jpa 2 hibernate limit (max results) to a CriteriaQ

2020-02-11 01:00发布

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

3条回答
Lonely孤独者°
2楼-- · 2020-02-11 01:17

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.

查看更多
萌系小妹纸
3楼-- · 2020-02-11 01:35

I usually use:

em.createQuery(criteria).setFirstResult(offset).setMaxResults(max).getResultList();
查看更多
对你真心纯属浪费
4楼-- · 2020-02-11 01:36

You could define the offset/limit like this:

return em.createQuery(query)
     .setFirstResult(offset) // offset
     .setMaxResults(limit) // limit
     .getResultList();
查看更多
登录 后发表回答