如何让 NHibernate 进行连接?

发布于 2024-09-10 21:12:18 字数 740 浏览 5 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海似她心 2024-09-17 21:12:18

您通过创建子标准

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

编辑:好的,我在发布后做了一些研究。看来我所做的代码应该可以工作,但会导致加载员工集合。相同的基本代码可以在 ayende 的博客上找到。那里有一个示例可以执行相同的操作,而不会导致集合重新加载。希望有帮助。

you join by creating sub criteria

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

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.

洋洋洒洒 2024-09-17 21:12:18

尝试:

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

Try:

Session.CreateCriteria(typeof(Store))
.CreateAlias("Employees", "e")
.Add(Restrictions.Not(Restrictions.Eq("e.SomeStatus1", true))
.List<Store>();
琴流音 2024-09-17 21:12:18

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

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

var result = query.ToList();

更多帮助此处< /a>.

I would suggest you use the Linq to NHibernate API instead of the Criteria API. With it, your query would be as follows:

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

var result = query.ToList();

More help here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文