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

查看:29
本文介绍了你如何在 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.

推荐答案

几年前,当被问及为什么这在 H​​ibernate 2 中有效,而在 Hibernate 3 中无效时,这是在 Hibernate 论坛上发布的:

Limit 从不 是受支持的子句在 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天全站免登陆