How to set limit for a hibernate query

安稳与你 提交于 2019-12-09 16:07:50

问题


How can I set a limit to this hql query? When I add the limit keyword in the query, an error is thrown.

 @Query("from voucher v where  v.voucherType.typeDescription = :typeDescription and v.denomination = :denomination")
 public List<Voucher> findByVoucherTypeAndDenomination(@Param("typeDescription") String typeDescription,@Param("denomination") BigDecimal denomination);

回答1:


When you call your query add the following:

.setFirstResult(firstResult).setMaxResults(limit);

UPDATE

Docs:
http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/Query.html#setMaxResults(int)

If you use entityManager, it can be like:

entityManager.createQuery("yourQuery").setFirstResult(0).setMaxResults(5);



回答2:


You can use two method of Query object.

setFirstResult()

setMaxResults()

but you can not use it in HQL because limit is database vendor dependent so hibernate doesn't allow it through hql query




回答3:


Limit was never a supported clause in HQL. You are meant to use setMaxResults().



来源:https://stackoverflow.com/questions/17735660/how-to-set-limit-for-a-hibernate-query

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