I have the following criteria.
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))
.SetResultTra开发者_运维技巧nsformer(CriteriaSpecification.DistinctRootEntity)
.List<DomainModels.Models.FreieAbrechnung>();
So I get this Sql-Statement:
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
The problem is, that the restrictions fp1_.IstAktiv = 1 is in the where clause. This restrictions have to be in the left outer join. Like this:
left outer join FreieAbrechnungPosition fp1_ on this_.ID=fp1_.FreieAbrechnungID and fp1_.IstAktiv = 1
What should I change in the criteria, that I get the right Sql-Statement?
Thanks, Dani
To add conditions to the join, you must use HQL instead of criteria.
A rough translation of your query would be:
select f
from FreieAbrechnung f
left join f.FreieAbrechnungPositionen fp with IstAktiv = 1
where f.IsAktiv = 1
... etc
精彩评论