nHibernate Eager Loading at runtime

早过忘川 提交于 2019-12-11 15:23:45

问题


We're using NCommon's UnitOfWorkScope which wraps nHibernate ISession functionality. Our goal is to eager-load complex properties on demand vs. always eagerly loading them via configuration. The idea is that a given service that retrieves an entity can be customized a bit by the calling code - sometimes we want only the parent entity to be hydrated, other times we may want the complex child properties hydrated, too.

To accomplish this, we're doing the following:

  var iSession = unitOfWorkScope.CurrentUnitOfWork<NHUnitOfWork>().GetSession<ParentEntity>();

  iSession.CreateCriteria<ParentEntity>().SetFetchMode("Children", FetchMode.Eager);

Once that's setup on the session, we use NCommon's IRepository functionality to retrieve the entities:

  var parent = parentRepository.FirstOrDefault(x => x.Id == 123);

However, when we check the parent.Children collection we get:

  NHibernate.Collection.Generic.PersistentGenericBag<ChildEntity>

... which tells me the Eager-loading did not occur. When we modify the NH mapping to always force an eager load, then we see the children correctly.

Can anyone shed light on this? I assume we're missing something since the FetchMode.Eager is ignored.


回答1:


This was the solution, found here:

http://slynetblog.blogspot.com/2011/11/in-spite-of-common-now-approach-of.html



来源:https://stackoverflow.com/questions/11420632/nhibernate-eager-loading-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!