【问题标题】:nHibernate Criteria for selecting all entities with empty child collections用于选择具有空子集合的所有实体的 nHibernate 标准
【发布时间】:2010-05-06 17:11:44
【问题描述】:

我很难编写标准来选择具有空子集合或空孙集合的所有实体。我可以将这些作为单独的 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);

【问题讨论】:

  • 怎么不行?你有例外吗?你是否得到了意想不到的结果?没有结果?
  • 返回错误的结果。

标签: nhibernate criteria


【解决方案1】:

我已经想出了一个解决方案,但不确定它是否是最好的(我应该购买 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多