2009-06-09 46 views
-2

我在HQL中为Hibernate编写了以下查询。HQL通过查询给出问题的顺序

============================================== ==========================

select new map(ret.retailerDesc as ret_name, ret.id.retailerId as ret_id, 
       ret.id.serviceId as service_id, 

(select count(distinct i.inspectionId) as inspections from Inspection i 
inner join i.clgCodeStatus c 
inner join c.retailerOrderses r 
inner join r.cusRetailer cr 
inner join i.inspectionMission m where ret.id = cr.id ) as inspections , 

(select count(distinct i.inspectionId) as inspections from Inspection i 
inner join i.clgCodeStatus c 
inner join c.retailerOrderses r 
inner join r.cusRetailer cr 
inner join i.inspectionMission m 
where ret.id = cr.id and i.inspectionResult = '1' ) as match, 

(select count(distinct i.inspectionId) as inspections from Inspection i 
inner join i.clgCodeStatus c 
inner join c.retailerOrderses r 
inner join r.cusRetailer cr 
inner join i.inspectionMission m 
where ret.id = cr.id and i.inspectionResult = '0' ) as mismatch ) 

from CusRetailer ret order by inspections desc 

================= ================================================== ====

当上面的查询执行它提供了以下错误:

ERROR: column "inspections" does not exist 

这是给这个错误“的检查倒序”。 如果我删除它可以正常工作。

任何人都可以请帮我解决这个问题吗?

谢谢。

+0

你好, 你能证明Hibernate自动生成的这个HQL查询的SQL(你可以通过show_sql属性设置为true使休眠打印)。 从错误看来,Hibernate并不像抱怨,但是你的数据库是。 干杯 – 2009-06-10 17:50:10

回答

0

我在上面的查询中使用“order by col_1_0_”解决了它,因为hibernate创建了名为col_0_0_,col_1_0_,col_2_0_等的列。所以如果您只需要知道列的顺序并将其添加到通过相应的顺序..

谢谢。

amar4kintu

+1

你确定这是一个很好的“解决方案”吗?如果您在查询的开头添加一列(或者其他一些更改会导致hibernate以某种方式重新排列列),那么您的排序将指向错误的列。 [这与使用顺序位置的人相同的概念(例如`ORDER BY 1`)](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/06/bad-habits-to-kick-为了逐ordinal.aspx)。当然,冬眠的所有荣耀都有比这种黑客更好的解决方案。如果没有,我更喜欢Gareth的建议。 – 2012-06-15 13:10:59

2

这可能是你需要重复表达了inspections

...from CusRetailer ret order by count(distinct i.inspectionId) 

这可能是HQL不支持order by条款中表述的情况下,你可能需要使用正SQL查询来代替。