We're almost there... following your suggestions
I come up with the following criteria
Code:
IList test = session.CreateCriteria(typeof(Participant), "p")
.CreateCriteria("p.DeliveryTickets", "dt", JoinType.LeftOuterJoin)
.CreateCriteria("p.DeliveryTickets.TemplateEmail", "te", JoinType.LeftOuterJoin)
.CreateCriteria("p.SurveyVersion", "sv", JoinType.LeftOuterJoin)
.CreateCriteria("p.Language", "lang", JoinType.InnerJoin)
.CreateCriteria("p.Country", "c", JoinType.InnerJoin)
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("p.Id"))
.Add(Projections.Property("p.Email"))
.Add(Projections.Property("sv.Name"))
.Add(Projections.Property("c.Name"))
.Add(Projections.Property("lang.Name"))
.Add(Projections.Property("dt.TicketStatus"))
.Add(Projections.Property("dt.TemplateEmail")))
.Add(Expression.Conjunction()
.Add(Expression.Eq("p.IsOnline", true))
.Add(Expression.IsNotNull("p.Email"))
.Add(Expression.Eq("p.Survey.Id", surveyId))
.Add(Expression.Disjunction()
.Add(Expression.IsNull("dt.Id"))
.Add(Subqueries.PropertyEq("dt.Id", lastParticipantTicket))))
.List();
Almost perfect! The criteria executes successfully but, not as expected.
Seems NHibernate is not respecting the JoinType I specify on my criterias.
From the criteria above, NHibernate generates the following SQL
Code:
SELECT
this_.Id as y0_
, this_.Email as y1_
, sv5_.Name as y2_
, c4_.Name as y3_
, lang3_.Name as y4_
, dt1_.status as y5_
, te2_.TemplateEmailKey as y6_
FROM Participant this_
inner join Country c4_ on this_.country_id=c4_.Id
inner join SurveyVersion sv5_ on this_.survey_version_id=sv5_.Id
inner join Language lang3_ on this_.language_id=lang3_.Id
left outer join DeliveryTicket dt1_ on this_.Id=dt1_.part_id
inner join TemplateEmail te2_ on dt1_.template_id=te2_.Id
WHERE (this_.is_online = @p0 and this_.Email is not null and this_.survey_id = @p1 and (this_.Id is null or dt1_.Id = (SELECT max(this_0_.Id) as y0_ FROM DeliveryTicket this_0_ WHERE this_0_.part_id = this_.Id))) ORDER BY dt1_.id DESC
But, the generated SQL should be something like this
Code:
SELECT
this_.Id as y0_
, this_.Email as y1_
, sv5_.Name as y2_
, c4_.Name as y3_
, lang3_.Name as y4_
, dt1_.status as y5_
, te2_.TemplateEmailKey as y6_
FROM Participant this_
inner join Country c4_ on this_.country_id=c4_.Id
inner join Language lang3_ on this_.language_id=lang3_.Id
left join SurveyVersion sv5_ on this_.survey_version_id=sv5_.Id
left join DeliveryTicket dt1_ on this_.Id=dt1_.part_id
left join TemplateEmail te2_ on dt1_.template_id=te2_.Id
WHERE (this_.is_online = @p0
and this_.Email is not null
and this_.survey_id = @p1
and (dt1_.Id = (SELECT max(this_0_.Id) as y0_ FROM DeliveryTicket this_0_ WHERE this_0_.part_id = this_.Id)
or dt1_.Id is null))
order by dt1_.[id]
Cheers,
Felipe Garcia