如何在 Hibernate 中设置内部查询限制?

发布于 2024-08-30 09:23:50 字数 201 浏览 0 评论 0原文

我有这样的 HQL:

from Table1 t1 where t1.name not in (select t2.name from Table2 t2 order by t2.date limit 10)

问题是它不理解 limit 关键字。有没有办法运行这样的查询而不将其分成两个子查询?

I have HQL like this:

from Table1 t1 where t1.name not in (select t2.name from Table2 t2 order by t2.date limit 10)

The problem is it doesn't understand limit keyword. Is there a way to run such query without splitting it into two subqueries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把昨日还给我 2024-09-06 09:23:50

看看 如何在 HQL 中进行限制查询?

你不能用 hql 来限制用 hql 编写的查询。您需要在 Query 对象上调用 setMaxResults,我想这会阻止您对 hql 子查询应用限制。

这让您可以选择将

  • 其编写为 sql 查询,或者
  • 尝试找到另一种方法来编写 hql 查询,这样您就不需要子查询中的限制。

look at How do you do a limit query in HQL?

you can't limit a query written in hql with hql. You need to make a call to setMaxResults on the Query object, which i guess will prevent you from applying a limit on a hql subquery.

This leave you with the option of

  • writting it as a sql-query or
  • trying to find another way to write your hql query so that you don't need a limit in a subquery.
爱已欠费 2024-09-06 09:23:50

如果您将查询作为 SQLQuery 提交,然后将类添加为实体,则可以使用 limit,因为查询是作为 sql 提交的。不过,您必须使用 sql 语法。

String sql = "select * from Supplier limit 1";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Supplier.class);
List results = query.list();

--> (也可以使用子选择)

If you submit the query as an SQLQuery and then add your class as an entity, you can use limit, as the query is submittet as sql. You have to use sql Syntax though.

String sql = "select * from Supplier limit 1";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Supplier.class);
List results = query.list();

--> (also working with subselects)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文