I'm having difficulty writing a Criteria to select all entities with empty child collections or empty grand-child collections. I can do these as separate Criteria but I'm having trouble combining into a single Criteria.
class structure:
public class Component
{
public IList<Version> Versions { get; set; }
}
public class Version
{
public IList<SubscribeEvent> SubscribedEvents { get; set; }
public IList<PublishEvent> PublishedEvent { get; set; }
}
This does not work:
return session
.CreateCriteria<Component>("c")
.CreateCriteria("Versions", "v")
.Add(Restrictions.Or(Restrictions.IsEmpty("c.Versions"), Restrictions.And(Restrictions.IsEmpty("v.PublishedEvents"),
Restrictions.IsEmpty("v.SubscribedEvents"))))
.SetCacheable(true);
I've managed to work out a solution, not sure if it is the best (I should buy NH profiler)
return session
.CreateCriteria<Component>("c")
.CreateAlias("Versions", "v", JoinType.LeftOuterJoin)
.Add(Restrictions.Or(Restrictions.IsEmpty("c.Versions"),
Restrictions.And(Restrictions.IsEmpty("v.SubscribedEvents"),
Restrictions.IsEmpty("v.PublishedEvents"))))
.SetCacheable(true);