在 NHibernate 中强制进行急切选择

发布于 2024-08-14 17:19:37 字数 504 浏览 10 评论 0原文

我正在尝试使用选择急切地获取集合,但我只是 获取是内部连接。到底是怎么回事?

Session.CreateCriteria(typeof(Foo))
    .SetFetchMode("Bars", FetchMode.Select)
    .CreateAlias("Bars", "b")
    .SetFetchMode("b.Bazes", FetchMode.Select)
    .List();

我尝试将 FetchMode 更改为 Eager 但这不起作用 - 我 仍然获得内部联接而不是单独的选择。我不确定它从哪里获得内部连接,因为文档中没有任何内容谈论 FetchMode 导致内部连接。 是否有可能获得急切的选择?

更新 好的,我发现创建别名会导致内部联接。因此我可以使用 .CreateAlias("Bars", "b", JoinType.None),但随后 b.Bazes 的获取将恢复为延迟加载。呃。

I am trying to eagerly fetch collections using selects, but all I am
getting is inner joins. What is going on?

Session.CreateCriteria(typeof(Foo))
    .SetFetchMode("Bars", FetchMode.Select)
    .CreateAlias("Bars", "b")
    .SetFetchMode("b.Bazes", FetchMode.Select)
    .List();

I have tried changing FetchMode to Eager but that doesn't work - I
still get inner joins instead of seperate selects. I'm not sure where it is even getting the inner join from, because nothing in the docs talks about FetchMode causing inner joins.
Is it possible to get eager selects?

Update
OK I worked out that creating an alias causes an inner join. So I can use .CreateAlias("Bars", "b", JoinType.None), but then the fetching of b.Bazes reverts to lazy loading. Urgh.

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

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

发布评论

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

评论(3

铃予 2024-08-21 17:19:37

INNER JOIN 是 NHibernate 加载记录及其相关子记录的方式。这通常是最有效的方法。

如果要使用多个 SELECT 语句,则对子项的查询将需要以某种方式包含父项的条件。 INNER JOIN 可以轻松获取相关子项,NHibernate 在运行查询后会正确地将其拆分为多个实体。

我相信 Entity Framework 4 会让您执行多个查询并“神奇地”重新附加相关对象,但我不知道 NHibernate 有这样的功能(我确信如果我在这方面错了,有人会纠正我) 。

An INNER JOIN is how NHibernate will load your records and their related child records. This is generally the most efficient way of doing it.

If it was to use multiple SELECT statements then the query for children would need to somehow include the criteria for the parent. An INNER JOIN makes it easy to get just related children, NHibernate will correctly split this out into multiple entities after running the query.

I believe Entity Framework 4 will let you do multiple queries and 'magically' re-attach related objects, but I'm not aware of NHibernate having such a feature (I'm sure someone will correct me if I'm wrong on this).

温柔女人霸气范 2024-08-21 17:19:37

对于 nhibernate 来说,使用左外连接来急切加载实体。因此,您需要像这样更改代码:

Session.CreateCriteria(typeof(Foo))
.SetFetchMode("Bars", FetchMode.Select)
.CreateAlias("Bars", "b", JoinType.LeftOuterJoin)
.SetFetchMode("b.Bazes", FetchMode.Select)
.List();

它在类似场景中帮助了我。

For nhibernate to eager load entities left outer join is used. So you need to change your code like this:

Session.CreateCriteria(typeof(Foo))
.SetFetchMode("Bars", FetchMode.Select)
.CreateAlias("Bars", "b", JoinType.LeftOuterJoin)
.SetFetchMode("b.Bazes", FetchMode.Select)
.List();

It helped me in similar scenario.

最好是你 2024-08-21 17:19:37
Session.CreateCriteria(typeof(Foo))
   .SetFetchMode("Bars", FetchMode.Select)
   .CreateAlias("Bars", "b")  <-- this line triggers a join ( think )
   .SetFetchMode("b.Bazes", FetchMode.Select) 
   .List();

如果您确实不想加入,您可以在映射中指定 fetch="select",但这会导致不推荐的 N+1 选择(每个 Foo 实体一个选择,然后每个 Baze 一个选择)

Session.CreateCriteria(typeof(Foo))
   .SetFetchMode("Bars", FetchMode.Select)
   .CreateAlias("Bars", "b")  <-- this line triggers a join ( think )
   .SetFetchMode("b.Bazes", FetchMode.Select) 
   .List();

If you really do not want joins you can specify fetch="select" in the mapping, but that would cause a N+1 select which is not recommended (one select for each Foo entity, and then one for each Baze)

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