wolli wrote:
Try this:
Code:
select a.Id, a.Title, c2.CreatedAt
from Article a left join fetch a.Comments c1
where c1.CreatedAt = (select max(c2.CreatedAt) from Comments c2 where c2.Article = a)
That worked great, I ended up converting it to use ICriteria (because additional criteria is coming from a specification to provide additional filtering) - so I ended up with:
DetachedCriteria maxCreateDateForComments = DetachedCriteria.For<Comment>("c2")
.Add(Restrictions.EqProperty("c2.Article", "c1.Article"))
.SetProjection(Projections.Max("c2.CreatedAt"));
DetachedCriteria criteria = DetachedCriteria.For<Article>("a")
.CreateAlias("Comments", "c1", JoinType.LeftOuterJoin)
.Add(Restrictions.Or(
Subqueries.PropertyEq("c1.CreatedAt", maxCreateDateForComments),
Restrictions.IsEmpty("a.Comments")));
Thanks for the help!