迭代 NHibernate 连接

发布于 2024-07-23 05:26:32 字数 1337 浏览 6 评论 0原文

这是我第一次在大型项目中使用 NHibernate,所以请耐心等待。 基本上,我需要基于 5 个字段进行搜索。

我想在表格中显示结果。 这是我编写的一个测试,基本上获取了所有与名为“DII”的员工有过接触的客户。 当我运行它时,我收到一条错误消息,指出某些 Intakes 为空。

var name = new Name("Alice", "B." "Cooper");
var staff = "DII";
var fileNumber = 12345;

var crit = NHibernateHelper.GetCurrentSession().CreateCriteria(typeof(Client));
var result = crit.CreateAlias("Intakes", "i");            
        .Add(Restrictions.Like("Name.First", name.First + "%"));
        .Add(Restrictions.Like("Name.Middle", name.Middle + "%"));
        .Add(Restrictions.Like("Name.Last", name.Last + "%"));   
        .Add(Restrictions.Eq("i.Staff", staff));            
        .Add(Restrictions.Eq("i.FileNumber", fileNumber));
        .List<Client>();

foreach (var client in result)
{
    Assert.IsTrue(client.Intakes.All(i => i.Staff == "DII");
}

但是,它确实返回了正确的客户端数量。 我的问题是,如何仅迭代由上述 ICriteria 生成的查询返回的关联 Intake?

我尝试将最后一行修改为:

Assert.IsTrue(c.Intakes.Where(i => i != null).All(i => i.Staff == "DII"));

它工作正常,除了生成的 SQL 是:

SELECT (Client + Intake Join) ...
SELECT FROM IntakeTable ...
SELECT FROM IntakeTable ...
SELECT FROM IntakeTable ...
(for each Intake that was returned with the join)

意味着它运行 Join,然后再次获取所有 Intake,这不是我想要的。

This is the first time I've used NHibernate for a big project so bear with me. Basically, I need to run a search based on 5 fields.

I want to display the results in a table. Here is a test I've written that basically gets all Clients that have Intakes with a Staff named "DII". When I run it, I get an error saying that some of the Intakes are null.

var name = new Name("Alice", "B." "Cooper");
var staff = "DII";
var fileNumber = 12345;

var crit = NHibernateHelper.GetCurrentSession().CreateCriteria(typeof(Client));
var result = crit.CreateAlias("Intakes", "i");            
        .Add(Restrictions.Like("Name.First", name.First + "%"));
        .Add(Restrictions.Like("Name.Middle", name.Middle + "%"));
        .Add(Restrictions.Like("Name.Last", name.Last + "%"));   
        .Add(Restrictions.Eq("i.Staff", staff));            
        .Add(Restrictions.Eq("i.FileNumber", fileNumber));
        .List<Client>();

foreach (var client in result)
{
    Assert.IsTrue(client.Intakes.All(i => i.Staff == "DII");
}

However, it does return the correct number of Clients. My question is, how do I only iterate through the associated Intakes that would've been returned by the query generated by the above ICriteria?

I tried modifying the last line to be:

Assert.IsTrue(c.Intakes.Where(i => i != null).All(i => i.Staff == "DII"));

And it works correctly, except that the SQL generated is:

SELECT (Client + Intake Join) ...
SELECT FROM IntakeTable ...
SELECT FROM IntakeTable ...
SELECT FROM IntakeTable ...
(for each Intake that was returned with the join)

Meaning the it runs the Join, and then fetches all the Intakes again, which is not what I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

江湖正好 2024-07-30 05:26:40

呵呵,想通了。 这是因为我的映射。 我正在使用:

<list ... />

...并按非位置列排序。 因此,我的 Intakes 关联中收到了一堆空条目。

我把它变成了一个订购的包,一切都如你所期望的那样。

Doh, figured it out. It was because of my mapping. I was using:

<list ... />

...and ordering by a non-position column. Because of that I was getting a bunch of null entries in my Intakes association.

I turned it into an ordered bag and everything worked as you'd expect.

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