nHibernate Eager在运行时加载 [英] nHibernate Eager Loading at runtime

查看:150
本文介绍了nHibernate Eager在运行时加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用NCommon的UnitOfWorkScope,它包装了nHibernate ISession的功能。我们的目标是根据需求急切加载复杂的属性,并始终通过配置来加载它们。这个想法是,一个给定的服务,检索一个实体可以定制一些调用代码 - 有时我们只需要父实体水合,其他时候我们可能希望复杂的子属性水合。 b
$ b

为了达到这个目的,我们做了以下工作:

  var iSession = unitOfWorkScope。 CurrentUnitOfWork< NHUnitOfWork>()的getSession< ParentEntity>(); 

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

在会话中设置好后,我们使用NCommon的IRepository功能来检索实体:

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

但是,当我们检查parent.Children集合时,我们得到:

  NHibernate.Collection.Generic.PersistentGenericBag< ChildEntity> 

...这告诉我没有发生Eager-loading。当我们将NH映射修改为始终强制加载时,我们会正确地看到这些孩子。

任何人都可以解释这一点吗?由于FetchMode.Eager被忽略,我们假设我们错过了一些东西。

解决方案

这是解决方案, p>

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


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.

解决方案

This was the solution, found here:

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

这篇关于nHibernate Eager在运行时加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆