我有一个休眠 HQL 问题。我想写一个子查询作为派生表(出于性能原因)。是否可以在 HQL 中做到这一点?例子:
FROM Customer WHERE country.id in
(SELECT id FROM (SELECT id FROM Country where type='GREEN') derivedTable)
(顺便说一句,这只是一个示例查询,所以不要就重写它提供建议,这只是我感兴趣的派生表概念)
不幸的是,派生表目前在 HQL 中不起作用。例如,以下工作:
List<int> result =
nHSession.CreateQuery( @"select distinct Id from User u")
.List<int>().ToList();
...以下抛出此异常:抛出了 “Antlr.Runtime.NoViableAltException”类型的异常。靠近第 1 行,第 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();
回退是创建一个包含原始 sql 的命名查询或创建一个存储过程并通过命名查询调用它,如下所示:
List<int> result = nHSession.GetNamedQuery("spUserIds")
.SetInt32("id", 3)
.List<int>().ToList();
您可以在我的博客http://blog.eyallupu.com/2009/07/hibernate-derived-properties.html 中找到有关派生属性和性能注意事项的一些信息
希望它会有所帮助,
Eyal Lupu