您如何在JPQL或HQL中进行限价查询? [英] How do you do a limit query in JPQL or HQL?

查看:155
本文介绍了您如何在JPQL或HQL中进行限价查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Hibernate 3中,有没有办法等效于以下HQL中的MySQL限制?

In Hibernate 3, is there a way to do the equivalent of the following MySQL limit in HQL?

select * from a_table order by a_table_column desc limit 0, 20;

如果可能,我不想使用setMaxResults.在旧版本的Hibernate/HQL中,这肯定是可能的,但它似乎已经消失了.

I don't want to use setMaxResults if possible. This definitely was possible in the older version of Hibernate/HQL, but it seems to have disappeared.

推荐答案

这是几年前在Hibernate论坛上发布的,当被问到为什么它在Hibernate 2中有效但在Hibernate 3中无效时

This was posted on the Hibernate forum a few years back when asked about why this worked in Hibernate 2 but not in Hibernate 3:

限制从不是受支持的子句 在HQL中.你注定要使用 setMaxResults().

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

因此,如果它在Hibernate 2中工作,那似乎是偶然的,而不是设计的.我认为这是因为Hibernate 2 HQL解析器将替换它识别为HQL的查询的位,而其余部分保持原样,因此您可以使用一些本机SQL.但是,Hibernate 3具有适当的AST HQL解析器,并且它的宽容度要低得多.

So if it worked in Hibernate 2, it seems that was by coincidence, rather than by design. I think this was because the Hibernate 2 HQL parser would replace the bits of the query that it recognised as HQL, and leave the rest as it was, so you could sneak in some native SQL. Hibernate 3, however, has a proper AST HQL Parser, and it's a lot less forgiving.

我认为Query.setMaxResults()确实是您唯一的选择.

I think Query.setMaxResults() really is your only option.

这篇关于您如何在JPQL或HQL中进行限价查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆