當我有類似下面的HQL東西行:左連接是過濾掉,我希望展示使用HQL
select count(distinct car.id),
count(distinct w1.id),
count(distinct w2.id),
count(distinct w3.id)
from Car car
left join Wheel w1
left join Wheel w2 with w2.IsFrontWheel == true
left join Wheel w3 with w3.IsFrontWheel == false
where w1.ManufacturedDate > :manufacturedDate
and w2.ManufacturedDate > :manufacturedDate
and w3.ManufacturedDate > :manufacturedDate
group by car.Make
這HQL被動態地通過我的用戶的選擇時,他們是在運行報表生成。這裏要注意的重要一點是,用戶正在動態選擇過濾,這會導致此HQL動態構建where子句。
我有where子句生成正常工作。
我的問題是,如果你運行在SQL Server 2008中生成的查詢,最終的結果是,如果有車輪(W1或者W2和W3)不會有任何 ManufacturedDate大於給定參數,該報告完全是,省略了該記錄的。
我發現此原因,in this article here。
我的問題是,我無法將這些過濾器添加到HQL中的with子句中。通過上面的例子,我可以將約束條件添加到with子句中,但是我創建的報表引擎讓用戶做了比這更高級的過濾,其中大部分內容在with子句中是不允許的。我曾嘗試將一些生成的HQL放入with中,並驗證確實如此,並且大多數查詢不會運行,因爲它們需要對其他表進行額外的約束。
所以我的問題是:
有什麼辦法來修改這個HQL得到了丟失記錄回來?在上面列出的文章中,有人提到如果考慮到NULL,你可以得到記錄,但我不知道他指的是什麼方式,如果這是可能的,我肯定想知道怎麼樣。
UPDATE
我被迫把動態過濾器在where子句中,因爲用戶還可以做這樣的事情:
select count(distinct car.id),
count(distinct w1.id),
count(distinct w2.id),
count(distinct w3.id)
from Car car
left join Wheel w1
left join Wheel w2 with w2.IsFrontWheel == true
left join Wheel w3 with w3.IsFrontWheel == false
where w1.Buyer.FirstName = :firstName
and w2.Buyer.FirstName = :firstName
and w3.Buyer.FirstName = :firstName
group by car.Make
這要是我在改寫與條款
select count(distinct car.id),
count(distinct w1.id),
count(distinct w2.id),
count(distinct w3.id)
from Car car
left join Wheel w1 with w1.Buyer.FirstName = :firstName
left join Wheel w2 with w2.IsFrontWheel == true
and w2.Buyer.FirstName = :firstName
left join Wheel w3 with w3.IsFrontWheel == false
and w3.Buyer.FirstName = :firstName
group by car.Make
不會運行,NHibernate會拋出異常。
。我知道有關關鍵字,我的例子甚至使用它,但你不能做的事情,如「與w1.Buyer.id(選擇ID從買家,其中FirstName =:firstName」 – Joseph 2010-11-19 14:06:59