问题
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