开发者

NHibernate 3 and Future with Lazy Load

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.Remind开发者_如何学编程er>();
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.


You can't override a lazy="false" in the mapping with SetFetchMode.

Personally, I recommend you never use lazy="false". Recommended read: NHibernate is lazy, just live with it

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜