nHibernate Criteria,用于选择具有空子集合的所有实体

时间:2010-05-06 17:11:45

标签: nhibernate criteria

我在编写Criteria时难以选择具有空子集合或空子集合的所有实体。我可以将它们作为单独的Criteria来完成,但是我在组合单个Criteria时遇到了麻烦。

班级结构:

public class Component
    {
        public IList<Version> Versions { get; set; }
    }

    public class Version
    {
        public IList<SubscribeEvent> SubscribedEvents { get; set; }
        public IList<PublishEvent> PublishedEvent { get; set; }
    }

这不起作用:

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);

1 个答案:

答案 0 :(得分:3)

我设法制定出一个解决方案,不确定它是否是最好的(我应该购买NH剖面仪)

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);