2009-06-09 47 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查詢來代替。