Hibernate HQL 中使用派生表的子查询

发布于 2024-08-24 21:42:53 字数 262 浏览 6 评论 0原文

我有一个 Hibernate HQL 问题。 我想将子查询编写为派生表(出于性能原因)。 在 HQL 中可以做到这一点吗? 示例:(

FROM Customer WHERE country.id in 
(SELECT id FROM (SELECT id FROM Country where type='GREEN') derivedTable)

顺便说一句,这只是一个示例查询,因此不要提供有关重写它的建议,这只是我感兴趣的派生表概念)

I have a Hibernate HQL question.
I'd like to write a subquery as a derived table (for performance reasons).
Is it possible to do that in HQL?
Example:

FROM Customer WHERE country.id in 
(SELECT id FROM (SELECT id FROM Country where type='GREEN') derivedTable)

(btw, this is just a sample query so don't give advice on rewriting it, is just the derived table concept I'm interested in)

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

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

发布评论

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

评论(2

可遇━不可求 2024-08-31 21:42:53

不幸的是,派生表目前无法在 HQL 中工作。
例如,以下内容有效:

List<int> result =
  nHSession.CreateQuery( @"select distinct Id from User u")
  .List<int>().ToList();

...以下内容抛出此异常:
抛出了“Antlr.Runtime.NoViableAltException”类型的异常。 1号线附近,
第 24 列 [select different Id from (select u from S2.BP.Model.User u)]

List<int> result = nHSession.CreateQuery(
    @"select distinct Id from (select u from User u)")
    .List<int>().ToList();

后退方法是创建一个包含原始 sql 的命名查询,或者创建一个存储过程并调用它通过命名查询,如下所示:

List<int> result = nHSession.GetNamedQuery("spUserIds")
    .SetInt32("id", 3)
    .List<int>().ToList();

Unfortunately no, derived tables don't currently work in HQL.
For example, the following works:

List<int> result =
  nHSession.CreateQuery( @"select distinct Id from User u")
  .List<int>().ToList();

...the following throws the this exception:
Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. near line 1,
column 24 [select distinct Id from (select u from S2.BP.Model.User u)]

List<int> result = nHSession.CreateQuery(
    @"select distinct Id from (select u from User u)")
    .List<int>().ToList();

The fall back would be to create a named query containing raw sql or to create a stored procedure and invoke it via named query, like so:

List<int> result = nHSession.GetNamedQuery("spUserIds")
    .SetInt32("id", 3)
    .List<int>().ToList();
花期渐远 2024-08-31 21:42:53

You can find some information about derived properties and performance considerations on my blog in http://blog.eyallupu.com/2009/07/hibernate-derived-properties.html

Hope it will help,
Eyal Lupu

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