Subquery using derived table in Hibernate HQL

冷暖自知 提交于 2019-12-04 08:16:21

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();

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!