NHibernate 标准 fetchmode

发布于 2024-09-28 06:44:17 字数 662 浏览 1 评论 0原文

我有一个类型 Type1,其属性 Type2s 由 List 组成。我已为该属性配置了 NHibernate 映射,如下所示:

[Map(0, Table = "Type2s", Schema = "MySchema", Cascade = CascadeStyle.All, Lazy = true, Inverse = true)]
[Key(1, Column = "Type1Id")]
[OneToMany(2, Class = "Type2, MyNamespace")]

这两个用于检索 Type1 实例的条件之间的行为有什么区别:

var criteria1 = DetachedCriteria.For<MyType1>();

var criteria2 = DetachedCriteria.For<MyType1>().SetFetchMode("Type2s", FetchMode.Join);

当我从这些条件的可执行条件上调用 .List 时,大概是检索与关联的 Type2 的 SQL每个 Type1 实例在我实际尝试访问该属性之前不会运行,因为该属性被标记为惰性。这是一个正确的假设吗?

如果我想强制评估 Lazy 属性,如何最好地实现这一点?

I have a type Type1 with a property Type2s consisting of a List<Type2>. I have configured the NHibernate mapping for the property as follows:

[Map(0, Table = "Type2s", Schema = "MySchema", Cascade = CascadeStyle.All, Lazy = true, Inverse = true)]
[Key(1, Column = "Type1Id")]
[OneToMany(2, Class = "Type2, MyNamespace")]

What is the difference in behavior between these two criteria for retrieval of Type1 instances:

var criteria1 = DetachedCriteria.For<MyType1>();

var criteria2 = DetachedCriteria.For<MyType1>().SetFetchMode("Type2s", FetchMode.Join);

When I invoke .List on the executable criteria from these criteria, presumably the SQL to retrieve the Type2s associated with each Type1 instance is not run until I actually attempt to access the property because the property is marked as being Lazy. Is this a correct assumption?

If I want to force evaluation of the Lazy property how can this best be achieved?

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

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

发布评论

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

评论(1

寄风 2024-10-05 06:44:17

我不太熟悉属性绑定与 hbm/flutter,但我相信你已经解决了你自己的问题!

您的假设是正确的,因为包是惰性的,所以在请求之前不会加载 Type2 的数据。您的第二个标准的 fetchmode 强制这些对象同时被水合,即。在一次数据库往返中。

顺便说一句,FetchMode.Join 相当于 FetchMode.Eager(我认为这是一个更好的名字 re:laziness)。

文章对此进行了更深入的解释:
http://davidhayden.com/blog/dave/archive/2008 /12/06/ImprovingNHibernatePerformanceFetchingStrategiesFetchModeFluentNHibernate.aspx

描述 FetchMode.JoinFetchMode.Eager
http:// /bchavez.bitarmory.com/archive/2008/04/04/differences- Between-nhibernate-fetchmode.eager-and-fetchmode.join.aspx

I'm not very familiar with attribute binding vs. hbm/fluent, but I believe you have solved your own problem!

You are correct assuming that the data for the Type2s will not be loaded until request because of the bag being lazy. Your second criteria's fetchmode forces those object to be hydrated at the same time, ie. in one database roundtrip.

As an aside, FetchMode.Join is equivalent to FetchMode.Eager (which is a better name re:laziness in my opinion).

article explaining it a little more in depth:
http://davidhayden.com/blog/dave/archive/2008/12/06/ImprovingNHibernatePerformanceFetchingStrategiesFetchModeFluentNHibernate.aspx

describing FetchMode.Join and FetchMode.Eager:
http://bchavez.bitarmory.com/archive/2008/04/04/differences-between-nhibernate-fetchmode.eager-and-fetchmode.join.aspx

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