NHibernate 标准 fetchmode
我有一个类型 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太熟悉属性绑定与 hbm/flutter,但我相信你已经解决了你自己的问题!
您的假设是正确的,因为包是惰性的,所以在请求之前不会加载 Type2 的数据。您的第二个标准的 fetchmode 强制这些对象同时被水合,即。在一次数据库往返中。
顺便说一句,FetchMode.Join 相当于 FetchMode.Eager(我认为这是一个更好的名字 re:laziness)。
文章对此进行了更深入的解释:
http://davidhayden.com/blog/dave/archive/2008 /12/06/ImprovingNHibernatePerformanceFetchingStrategiesFetchModeFluentNHibernate.aspx
描述
FetchMode.Join
和FetchMode.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
andFetchMode.Eager
:http://bchavez.bitarmory.com/archive/2008/04/04/differences-between-nhibernate-fetchmode.eager-and-fetchmode.join.aspx