NHibernate 3 和未来的延迟加载
我有一个包含时间表集合的实体提醒(请参阅映射 这里)。 我不希望我的集合 Schedules 延迟加载,因此我将属性设置为 false:
<set access="field.pascalcase-underscore" cascade="all-delete-orphan" inverse="true" lazy="false" name="Schedules" mutable="true">
<key foreign-key="FK_Schedules_Reminders">
<column name="ReminderCode" />
</key>
<one-to-many class="ReminderSchedule" />
</set>
在一个查询中,我不想加载集合,因此我将 SetFetchMode 设置为 Lazy:
ICriteria FiltersCriteria = session.CreateCriteria<Domain.Reminder>();
FiltersCriteria.SetFetchMode("Schedules", FetchMode.Lazy);
运行我的查询,我注意到 Nhibernate 是运行提醒查询,然后,对于每个提醒,它运行单个查询来获取时间表。
这是我正在使用的代码:
ICriteria FiltersCriteria = session.CreateCriteria<Domain.Reminder>();
FiltersCriteria.SetFetchMode("Schedules", FetchMode.Lazy)
Result = FiltersCriteria.List<Domain.Reminder>();
经过一番努力,我使用 Future 更改了我的代码,一切正常:
ICriteria FiltersCriteria = session.CreateCriteria<Domain.Reminder>();
FiltersCriteria.SetFetchMode("Schedules", FetchMode.Lazy)
Result = FiltersCriteria
.Future<Domain.Reminder>()
.ToList<Domain.Reminder>();
为什么它不能简单地使用 FetchMode.Lazy 工作?为什么我必须使用未来? 如果我在模式中设置惰性模式,显然,一切都会按我的预期进行。
谢谢。
I've got an entity Reminder which contains a collection of Schedules (see the mapping here).
I didn't want my collection Schedules to be lazy loaded so I set the attribute to false:
<set access="field.pascalcase-underscore" cascade="all-delete-orphan" inverse="true" lazy="false" name="Schedules" mutable="true">
<key foreign-key="FK_Schedules_Reminders">
<column name="ReminderCode" />
</key>
<one-to-many class="ReminderSchedule" />
</set>
In one query I didn't want to load the collection so I've set the SetFetchMode to Lazy:
ICriteria FiltersCriteria = session.CreateCriteria<Domain.Reminder>();
FiltersCriteria.SetFetchMode("Schedules", FetchMode.Lazy);
Running my query I noticed that Nhibernate was running the query for the reminders and then, for each reminder, it was running single queries to fetch the Schedules.
Here is the code I was using:
ICriteria FiltersCriteria = session.CreateCriteria<Domain.Reminder>();
FiltersCriteria.SetFetchMode("Schedules", FetchMode.Lazy)
Result = FiltersCriteria.List<Domain.Reminder>();
After a little bit of struggle I've changed my code using the Future and everything worked fine:
ICriteria FiltersCriteria = session.CreateCriteria<Domain.Reminder>();
FiltersCriteria.SetFetchMode("Schedules", FetchMode.Lazy)
Result = FiltersCriteria
.Future<Domain.Reminder>()
.ToList<Domain.Reminder>();
Why doesn't it work simply using FetchMode.Lazy? Why do I have to use Future?
If I set the lazy mode in my schema, obviously, everything works as I would expect.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用
SetFetchMode
覆盖映射中的lazy="false"
。就我个人而言,我建议您永远不要使用
lazy="false"
。推荐阅读:NHibernate 很懒,就忍受它吧You can't override a
lazy="false"
in the mapping withSetFetchMode
.Personally, I recommend you never use
lazy="false"
. Recommended read: NHibernate is lazy, just live with it