且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Hibernate Mapping问题与不相关的集合

更新时间:2021-11-25 22:54:05

经过一番研究,问题出现在Hibernate中,它被称为bug #HHH-2862

After some researches, the problem is in the Hibernate it is known as bug #HHH-2862


这基本上是由于拥有一个热切集合,其中集合的键在结果中不唯一。

It is basically caused by having an eager collection where the key of the collection is not unique in the results.

当Hibernate使用'persistenceContext.addUninitializedCollection()'初始化集合时,这将检测到具有给定键的集合已经被添加,然后设置old实例为null和当前的集合。然而,该集合已经通过较早的调用添加到持久化上下文中,并且当 StatefulPersistenceContext.initializeNonLazyCollections()遍历持久上下文中的所有集合调用 forceInitialization()上的引用命中null引用抛出一个集合没有与任何会话关联异常这就解释了为什么在我的情况下,只有最后一个对象有引用并且只有一个工作正常,以及你对延迟初始化问题的假设。

When Hibernate initialize collection using 'persistenceContext.addUninitializedCollection()' and this will detect that the collection with the given key has already been added, then sets old instance to null and current to the collection. However, that collection has already been added to the persistence context by an earlier call, and when StatefulPersistenceContext.initializeNonLazyCollections() iterate over all of the collections in the persistent context calling forceInitialization() on the references hit the null reference which throws a "collection is not associated with any session" exception". And this explain why in my case only the last object has the reference and with only one everything was working fine, and yours assumptions regarding the lazy initialization problem.

有些想法如何绕过这个bug?

Some thoughts how to bypass this bug ?