繁体   English   中英

如何让NHibernate加入?

[英]How do I get NHibernate to do a join?

我已经使用Fluent NHibernate来连接商店和员工类,其中Stores可以拥有许多员工,如下所示:

public class Store
{
    public virtual IList<Employee> Employees { get; set; }
    //other store properties
}

public class Employee
{
    public virtual Store Store { get; set; }   
    public virtual bool? SomeStatus1 { get; set; }
}

我需要让所有员工都没有将SomeStatus1设置为true。

我在这里的可行尝试失败了:

Session.CreateCriteria(typeof(Store))
    .Add(Restrictions.Not(Restrictions.Eq("Employees.SomeStatus1", true))
    .List<Store>();

知道我是怎么做的吗?

我的尝试失败的原因是因为列表Employees没有SomeStatus1的属性......这是相当明显的。

我不知道的是,如何让NHibernate只获得在我所寻找的州拥有员工的商店......

我想我想问NHibernate的是加入...但我不知道如何要求它这样做......

您通过创建子标准加入

var criteria = Session.CreateCriteria(typeof(Store));
var join = criteria.CreateCriteria("Employees");
join.Add(Restrictions.Not(Restrictions.Eq("SomeStatus1", true));
return criteria.List<Store>();

未经测试(obv)希望它有效,但你明白了。 这就是我用N:1做的,但你有1:N

编辑:好的,我发帖后做了一些研究。 似乎我所做的代码应该可以工作,但会导致加载employees集合。 ayende的博客上可以找到相同的基本代码。 那里有一个样本做同样的事情而不会导致重新加载集合。 希望有所帮助。

尝试:

Session.CreateCriteria(typeof(Store))
.CreateAlias("Employees", "e")
.Add(Restrictions.Not(Restrictions.Eq("e.SomeStatus1", true))
.List<Store>();

我建议您使用Linq to NHibernate API而不是Criteria API。 有了它,您的查询将如下所示:

var query = Session.Linq<Store>()
    .Where(store => store.SomeStatus1 != true);

var result = query.ToList();

这里有更多帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM