你如何在HQL中进行限制查询? [英] How do you do a limit query in HQL?

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

问题描述

在Hibernate 3中,是否有办法在HQL中执行相当于以下MySQL限制的操作?

  select * from按a_table_column desc限制0,20的a_table顺序; 

如果可能,我不想使用setMaxResults。这在Hibernate / HQL的旧版本中是绝对有可能的,但它似乎已经消失。

这是在几年前的Hibernate论坛上发布的,当被问及为什么这会起作用时Hibernate 2但不在Hibernate 3中:


HQL中的限制是 never 支持的子句
。您打算使用
setMaxResults()。

因此,如果它在Hibernate 2中运行,似乎是巧合而不是通过设计。我认为这是因为Hibernate 2 HQL解析器会替换它被识别为HQL的查询的位,并保持原来的状态,所以你可以潜入某些本地SQL中。然而,Hibernate 3有一个合适的AST HQL解析器,它的宽容度要低得多。



我认为 Query.setMaxResults()真的是你唯一的选择。


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;

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.

解决方案

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:

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

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.

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

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

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