2010-11-14 42 views
1

我有以下條件。NHibernate左加入限制

session.CreateCriteria<DomainModels.Models.FreieAbrechnung>() 
     .CreateAlias("FreieAbrechnungPositionen", "fp", JoinType.LeftOuterJoin)            
     .Add(Restrictions.Eq("fp.IstAktiv", true)) 
     .Add(Restrictions.Eq("Kunde.ID", kundenId)) 
     .Add(Restrictions.Eq("IstAktiv", true)) 
     .Add(Restrictions.Eq("Abgeschlossen", false)) 
     .SetResultTransformer(CriteriaSpecification.DistinctRootEntity)            
     .List<DomainModels.Models.FreieAbrechnung>(); 

所以我得到這個的SQL語句:

SELECT this_.ID as ID13_1_, this_.Version as Version13_1_, 
this_.KundeID as KundeID13_1_, this_.Erstellungsdatum as Erstellu4_13_1_, 
this_.Druckdatum as Druckdatum13_1_, this_.Abgeschlossen as Abgeschl6_13_1_, 
this_.AbgeschlossenDatum as Abgeschl7_13_1_, this_.IstAktiv as IstAktiv13_1_, 
this_.ErstelltDurchID as Erstellt9_13_1_, this_.ErstelltAm as ErstelltAm13_1_, 
this_.MutationDurch as Mutatio11_13_1_, this_.MutationAm as MutationAm13_1_, 
fp1_.FreieAbrechnungID as FreieAbr3_3_, fp1_.ID as ID3_, fp1_.ID as ID12_0_, 
fp1_.Version as Version12_0_, fp1_.FreieAbrechnungID as FreieAbr3_12_0_, 
fp1_.KundeID as KundeID12_0_, fp1_.Bezeichnung as Bezeichn5_12_0_, 
fp1_.Betrag as Betrag12_0_, fp1_.Erstellungsdatum as Erstellu7_12_0_, 
fp1_.MwStSteuerCodeID as MwStSteu8_12_0_, fp1_.IstAktiv as IstAktiv12_0_, 
fp1_.ErstelltDurchID as Erstell10_12_0_, fp1_.ErstelltAm as ErstelltAm12_0_, 
fp1_.MutationDurch as Mutatio12_12_0_, fp1_.MutationAm as MutationAm12_0_ 
FROM 
FreieAbrechnung this_ 
left outer join FreieAbrechnungPosition fp1_ on this_.ID=fp1_.FreieAbrechnungID 
WHERE 
fp1_.IstAktiv = 1 and this_.KundeID = 1 and this_.IstAktiv = 1 and this_.Abgeschlossen = 0 

的問題是,這些限制fp1_.IstAktiv = 1是where子句。這個限制必須在左外連接中。像這樣:

left outer join FreieAbrechnungPosition fp1_ on this_.ID=fp1_.FreieAbrechnungID and fp1_.IstAktiv = 1 

我應該在條件中更改哪些內容,以便獲得正確的Sql語句?

感謝, 達尼

回答

0

要條件添加到一起,你必須使用,而不是標準的HQL。

查詢的粗略的翻譯是:

select f 
from FreieAbrechnung f 
left join f.FreieAbrechnungPositionen fp with IstAktiv = 1 
where f.IsAktiv = 1 
... etc 
+0

感謝。我如何用FreieAbrechnung加載FreieAbrechnungPositionen?我不能這樣做:select f,fp from ... – Dani 2010-11-14 19:12:58

+0

left join ** fetch ** f.FreieAbrechnungPositionen fp等 – 2010-11-14 19:17:18

+0

現在我有這個例外:with-clause不允許獲取關聯;使用過濾器。但我不知道如何將濾鏡設置爲FreieAbrechnungPositionen而不是with-clause。 – Dani 2010-11-14 20:25:37