如何让 NHibernate 进行连接?
我使用 Fluent NHibernate 连接商店和员工类,其中商店可以有许多员工,如下所示:
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 是进行连接...但我不'不知道如何要求它这样做......
I've used Fluent NHibernate to hook up a store and employee class where Stores can have many employees as follows:
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; }
}
I'm needing to get all stores that have employees that do not have SomeStatus1 set to true.
My feable attempt here has failed:
Session.CreateCriteria(typeof(Store))
.Add(Restrictions.Not(Restrictions.Eq("Employees.SomeStatus1", true))
.List<Store>();
Any idea how I go about doing that?
The reason my attempt has failed is because the list Employees doesn't have a property of SomeStatus1...which is fairly obvious.
What I dont know, is how to get NHibernate to only get stores which have employees in the state I'm looking for...
I think what I'm wanting to ask NHibernate is to do a join...but I don't know how to ask it to do that...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您通过创建子标准
“未经测试”(obv) 来加入,希望它有效,但您明白了。这就是我用 N:1 做到的,但你有 1:N
编辑:好的,我在发布后做了一些研究。看来我所做的代码应该可以工作,但会导致加载员工集合。相同的基本代码可以在 ayende 的博客上找到。那里有一个示例可以执行相同的操作,而不会导致集合重新加载。希望有帮助。
you join by creating sub criteria
Untested (obv) hope it works, but you get the idea. That's how I do it with N:1 but you have 1:N
EDIT: Ok, I did a bit of research after posting. It seems the code I did should work, but will cause loading of the employees collection. The same basic code is found on ayende's blog. There is a sample there which does the same thing without causing the collection to be reloaded. Hope that helps.
尝试:
Try:
我建议您使用 Linq to NHibernate API 而不是 Criteria API。有了它,您的查询将如下所示:
更多帮助此处< /a>.
I would suggest you use the Linq to NHibernate API instead of the Criteria API. With it, your query would be as follows:
More help here.