1

我有以下标准。

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-Statement,我应该更改哪些标准?

谢谢,丹妮

4

1 回答 1

0

要向联接添加条件,您必须使用 HQL 而不是条件。

您的查询的粗略翻译是:

select f
from FreieAbrechnung f
left join f.FreieAbrechnungPositionen fp with IstAktiv = 1
where f.IsAktiv = 1
... etc
于 2010-11-14T16:45:39.497 回答