是否有可能编写hql查询,以通过一些子查询命令? 有这样的查询:订购休眠查询与选择
select a from A.a join a.b where a.b.id = :id
我需要的是这样的:
select a from A.a join a.b where a.b.id = :id order by (select count(c) from C c where c.a = a.id) desc
是否有可能编写hql查询,以通过一些子查询命令? 有这样的查询:订购休眠查询与选择
select a from A.a join a.b where a.b.id = :id
我需要的是这样的:
select a from A.a join a.b where a.b.id = :id order by (select count(c) from C c where c.a = a.id) desc
我的锯齿系统似乎并不奏效。或者,它可能是与Hibernate的错误,但这个事情的作品(虽然非常低效的),我有硬编码
select a, (select count(c) from C c where c.a = a.id) as col_1_0_
from from A.a join a.b where a.b.id = :id
order by col_1_0_
我使用Hibernate 3和MySQL
您可能需要观察为此hql生成的hibernate sql以确定别名。 – 2012-07-26 12:36:42
因为我认为没有更好的方法来做我想问的东西,所以我接受了这个答案(即使我不认为在生产中有这样的好处,但这是另一个讨论)。 – Dainius 2012-07-30 07:18:45
别名问题已在Hibernate 3.6.0 RC2中修复https://hibernate.atlassian.net/browse/HHH-1902 – 2013-04-24 13:21:18
别名按我的知识排序完成在形成选择的表格中的列上。如果错了,请纠正我。 – 2012-07-26 12:08:52
如果我使用本机查询它的工作原理,但我找不到如何处理hql。所以,也许你是对的,只有当你有直接的关系时才有可能(计数,平均等应该工作)。 – Dainius 2012-07-26 12:32:52