通过 NHibernate 使用无状态会话急切地获取曾孙集合
我正在使用 NHibernate 无状态会话将批量数据加载到数据库中。加载数据时,后面的实体需要查找以前的实体,以便将它们添加到子集合中。此操作涉及需要孙对象的数据,而孙对象又需要可用的曾孙集合。
标准如下所示:
var result = InternalRepository.CreateCritera<Root>()
.SetResultTransformer(Transformers.DistinctRootEntity)
.Add(Restrictions.IdEq(id))
.SetFetchMode("Child", FetchMode.Eager)
.CreateAlias("Child", "a", JoinType.LeftOuterJoin)
.SetFetchMode("a.Grandchild", FetchMode.Eager)
.CreateAlias("Grandchild", "b", JoinType.LeftOuterJoin)
.SetFetchMode("b.GreatGrandchildCollection", FetchMode.Eager)
.UniqueResult<Root>();
当我执行此操作时,TwoPhaseLoad
在 InitializeEntity
期间抛出异常,因为会话的持久性上下文实体条目为空:
at NHibernate.Engine.TwoPhaseLoad.InitializeEntity(Object entity, Boolean readOnly, ISessionImplementor session, PreLoadEvent preLoadEvent, PostLoadEvent postLoadEvent) in TwoPhaseLoad.cs: line 64
at NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session, Boolean readOnly) in Loader.cs: line 603
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in Loader.cs: line 472
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in Loader.cs: line 243
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in Loader.cs: line 1694
...
加载程序正在查找的实体是子实体。为什么这里的持久化上下文条目映射是空的?显然正在获取对象(生成正确的 SQL 并返回正确的结果),并且“Child”实体已正确创建。为什么实体构建状态不正确?这是否与无状态会话在急切加载期间如何使用临时持久性上下文有关?
I'm using an NHibernate Stateless Session to load bulk data into the database. As data is loaded, later entities need to lookup previous entities in order to add them to child collections. This operation involves needing data on a grandchild object, which in turn needs a great-grandchild collection to be available.
The criteria looks like this:
var result = InternalRepository.CreateCritera<Root>()
.SetResultTransformer(Transformers.DistinctRootEntity)
.Add(Restrictions.IdEq(id))
.SetFetchMode("Child", FetchMode.Eager)
.CreateAlias("Child", "a", JoinType.LeftOuterJoin)
.SetFetchMode("a.Grandchild", FetchMode.Eager)
.CreateAlias("Grandchild", "b", JoinType.LeftOuterJoin)
.SetFetchMode("b.GreatGrandchildCollection", FetchMode.Eager)
.UniqueResult<Root>();
When I execute this, TwoPhaseLoad
throws an exception during InitializeEntity
since the session's persistence context entity entries are empty:
at NHibernate.Engine.TwoPhaseLoad.InitializeEntity(Object entity, Boolean readOnly, ISessionImplementor session, PreLoadEvent preLoadEvent, PostLoadEvent postLoadEvent) in TwoPhaseLoad.cs: line 64
at NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session, Boolean readOnly) in Loader.cs: line 603
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in Loader.cs: line 472
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in Loader.cs: line 243
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in Loader.cs: line 1694
...
The entity the loader is looking up is the Child entity. Why is the persistence context entries map empty here? The object is apparently being fetched (the correct SQL is generated and returns correct results), and the "Child" entity is created correctly. Why is the entity construction state incorrect? Does it have to do with how the stateless session uses the temporary persistence context during eager loads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来这是无状态会话在两阶段加载期间管理临时持久性上下文的方式的限制。显然,Hibernate 中存在一个尚未移植到 NHibernate 的修复程序。
https://issues.jboss.org/browse/JBPAPP-3737
更新:
是的,上面的问题也是 NHibernate 中的问题。我在这里提交了一个补丁: https://nhibernate.jira.com/browse/NH-2669
It looks like it is a limitation in how the stateless session manages the temporary persistence context during the two phase load. Apparently, a fix exists in Hibernate which has not been ported over to NHibernate.
https://issues.jboss.org/browse/JBPAPP-3737
Update:
Yes, the above issue was the problem in NHibernate as well. I've submitted a patch here: https://nhibernate.jira.com/browse/NH-2669