Hibernate Mapping问题与不相关的集合 [英] Hibernate Mapping problem with unrelated collection

查看:147
本文介绍了Hibernate Mapping问题与不相关的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欢迎,

我有一些Hibernate映射的问题。

I've some problem with Hibernate mapping.

数据库结构

TableA
 -ID_A --PK

TableB
 -ID_B --PK
 -ID_A -- FK -> TableA

TableC
 -ID_C -- PK
 -ID_A -- FK -> TableA

POJO结构

class TableA extends Pojo {

 /*Some Fields*/

}

class TableB extends Pojo {

  TableA tableA; 

 /*Some properties*/

}

class TableC extends Pojo {

 TableA tableA;

 Collection<tableB> tableBs;

}






我想要的是在TableC Pojo的映射中的TableB元素的集合,映射关键是tableA。


What i want to have is the collection of TableB elements in mapping for a TableC Pojo, the mapping key is the tableA.

此集合应为只读。

映射应为hbm not annotations。

The mapping should be hbm not annotations.

我可能为每一种可能的方式做了...关闭我得到的情况下,当我操作一个TableC对象,然后一切正确,但如果我加载收集他们然后只有最后一个有适当的收集集。

I have probably done this for every possible way... the closes i get is case that when i operate on one TableC object then is everything correct but if i load the collection of them then only the last one have proper collection set.

更新:案例描述。

用例1:加载TableC的单个对象

The use case 1: Loading single object of TableC

Session session = (Session) getHibernateTemplate().getSessionFactory().openSession();
SQLQuery sqlQuery = session.createSQLQuery("SELECT c.* FROM TableC c WHERE c.ID_C = 1"); //Oracle
  sqlQuery.addEntity("c", TableC.class);
return sqlQuery.list(); //Return list with sigle object of TableC

在这种情况下,一切都可以正常工作。该对象加载了TableB对象列表中的所有数据和正确的项目。
在这个对象上,我们可以操作,更改和更新数据库中的修改。

In this case everything works fine as should. The object is loaded with all data and proper items in list of TableB objects. On this object we can operate, change it and update the modifications in database.

用例2加载对象集合

Session session = (Session) getHibernateTemplate().getSessionFactory().openSession();
SQLQuery sqlQuery = session.createSQLQuery("SELECT c.* FROM TableC c WHERE c.ID_C in (1,2)"); //Oracle
  sqlQuery.addEntity("c", TableC.class);
return sqlQuery.list(); // throws "collection is not associated with any session" 

在这种情况下,Hibernate在检索时抛出异常下一个对象。

In this case Hibernate throw an exception while retrieving next object.

*代码只是示例,会话将关闭。

*the codes are only samples, the session is closed after all.

推荐答案

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

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 ?

这篇关于Hibernate Mapping问题与不相关的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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