休眠:实体副本已被分配给不同的实体 [英] Hibernate: An entity copy was already assigned to a different entity

查看:116
本文介绍了休眠:实体副本已被分配给不同的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HiAll,



我现在试着理解,在我的编码和实体模型中出了什么问题
我只用hibernate 4.2来进行CRUD操作通用DAO模式和具有级联ALL和orphanRemoval = true的注释实体。
我拥有与DayPlanElement弱实体具有OneToMany关系的实体。 DayPlanElement 被保存到java.util.Set中。每个 DayPlanElement 都有一个属性order。


$ b DayPlan 也具有OneToMany关系,人。实体 Person 保存到java.util。 List 中。


$ b DayPlanElement DayPlanElementEntry 弱实体具有OneToMany关系。 java.util.Set用于保存。
Person 实体也与弱实体具有OneToMany关系。
java.util.Set用于保存。
DayPlanElement和DayPlanElement实体拥有由我的应用程序管理的ID作为String。
DayPlanElementEntry 弱实体具有用EmbeddedId注释的组合Id:DayPlanElementEntryId,其中包含parentPersonId和dayPlanElementId。它换句话说,想象一下,存在一个表格,代表了日程安排。这些列是从0到24的小时数。行是人员,必须从日程计划中进行操作。每列都是DayPlanElement实体。每一行都是Person实体。每个单元格都是DayPlanElementEntry实体。

如果我只是添加到表格新元素(也包括人员)并删除它(也从列表中删除它,然后调用DayPlanDAO。合并(dayPlan) - 我希望在级联和orphanRemoval) - 我没有问题。



只有当我尝试重新排列给定的人员(只是删除java.util.List中的操作)和调用DayPlanDAO.merge(dayPlan)时 - 下面的excepion将会抛出:

 导致:java.lang.IllegalStateException:
存储实体
时发生错误[DayPlanElementEntry [getDayPlanMode()= NONE,getCompositeId()= DayPlanElementEntryId [parentPersonId = 874c8eac-8796-478d-a4d5-dd011f7d6a4b,dayPlanElementId = ab683a25-633e-419e-89b6-4aef7829d4f6],hashCode = -2039940039]]。

实体副本
[org.hw.domain.DayPlanElementEntry#DayPlanElementEntryId [parentPersonId = 874c8eac-8796-478d-a4d5-dd011f7d6a4b,dayPlanElementId = ab683a25-633e-419e-89b6-4aef7829d4f6] ]

已被分配给不同的实体

[org.hw.domain.DayPlanElementEntry#DayPlanElementEntryId [parentPersonId = 874c8eac-8796-478d-a4d5-dd011f7d6a4b,dayPlanElementId = ab683a25 -633e-419E-89b6-4aef7829d4f6]。

at org.hibernate.event.internal.EventCache.put(EventCache.java:192)

at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener。 java:285)

at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)






我讨论了EventCache,寻找问题解决方案,只检查实现的equals()和hashCode()方法 - 没有成功: - ((
注意,我使用的是equals()方法的Id,例如DayPlanElementEntry的复合id,这个id被hashCode()使用,我只是不明白错误信息,三次输出相同的属性 - ids!



我找到了测试类为事件缓存,但现在我不明白以下你se case test:

  @Test 
public void testCopyAssociatedWith2ExistingEntities(){
session.getTransaction()。开始();
简单的entity1 = new Simple(1);
session.persist(entity1);
简单copy1 =新简单(1);
cache.put(entity1,copy1);
简单的实体2 =新建简单(2);
session.persist(entity2);
简单copy2 =新简单(2);
cache.put(entity2,copy2);
session.flush();

尝试{
cache.put(entity1,copy2);
失败(应抛出IllegalStateException);
}
catch(IllegalStateException ex){
// expected
assertTrue(ex.getMessage()。startsWith(Error occurred while stored entity [org.hibernate.event.internal .EventCacheTest $ Simple#1]。));
}
session.getTransaction()。rollback();
}

....

  @Entity 
private static class Simple {
@Id
private int value;

public Simple(int value){
this.value = value;
}

public int getValue(){
返回值;
}

public void setValue(int value){
this.value = value;


$ / code $ / pre

$ hr

我们使用 Id = 1 的简单实体entity1,然后我们坚持它。然后我们创建Simple entity copy1并将其作为值放入事件缓存中。作为使用的持久实体* 1 *的关键。
然后我们用 Id = 2 创建一个简单的实体* 2 *,然后创建副本* 2 * ID为2的实体,并将其作为值放入事件缓存中。作为使用的持久 entity2 的关键。
为什么这样的情况是错误的,为什么会出现这样的情况:IllegalStateException必须被抛出?

感谢您的帮助!
<解决方案

对于Hibernate 4.3.4版本以及其他版本,有一个修复。只需添加此工件即可:

 <依赖关系> 
< groupId> com.intersult< / groupId>
< artifactId> jpa-fix< / artifactId>
< version> 1.1< / version>
< /依赖关系>

这个修补程序将MergeEventListener替换为处理此类合并的MergeEventListener。


HiAll,

i try currently to understand, what is wrong in my coding and entity model I use the hibernate 4.2 to for CRUD operation with just Generic DAO pattern and annotated entities with cascading ALL and orphanRemoval= true. I have the DayPlan entity with OneToMany relationship to DayPlanElement weak entity. The DayPlanElement are saved into java.util.Set. Each DayPlanElement has a property "order".

The DayPlan has also OneToMany relationship to an entity Person. The entity Person saved into java.util.List.

The DayPlanElement has OneToMany relationship to DayPlanElementEntry weak entity. java.util.Set used for save. The Person entity also OneToMany relationship to DayPlanElementEntry weak entity. java.util.Set used for save. The DayPlan, DayPlanElement and Person entities have the ID, managed by my application, as String. The DayPlanElementEntry weak entity has a composite Id: DayPlanElementEntryId annotated with EmbeddedId, contains the parentPersonId and dayPlanElementId. It

In other words imagine, that exists a table, represents the day plan. The columns are the hours from 0 up to 24. The rows are the persons, that must proceed the operations from day plan. Each column would be DayPlanElement entity. Each row would be Person entity. And each cell would be DayPlanElementEntry entity.

If i just add to table new elements (also Persons) and remove its (also remove its from List and then call the DayPlanDAO.merge(dayPlan) - i hope on cascading and orphanRemoval) - i have no problem.

Only if i try to reorder the given Persons (just removing operations within java.util.List) and the call DayPlanDAO.merge(dayPlan) - the following excepion will be thrown:

Caused by: java.lang.IllegalStateException: 
Error occurred while storing entity 
[DayPlanElementEntry [getDayPlanMode()=NONE, getCompositeId()=DayPlanElementEntryId [parentPersonId=874c8eac-8796-478d-a4d5-dd011f7d6a4b, dayPlanElementId=ab683a25-633e-419e-89b6-4aef7829d4f6], hashCode=-2039940039]]. 

An entity copy 
[org.hw.domain.DayPlanElementEntry#DayPlanElementEntryId [parentPersonId=874c8eac-8796-478d-a4d5-dd011f7d6a4b, dayPlanElementId=ab683a25-633e-419e-89b6-4aef7829d4f6]] 

was already assigned to a different entity 

[org.hw.domain.DayPlanElementEntry#DayPlanElementEntryId [parentPersonId=874c8eac-8796-478d-a4d5-dd011f7d6a4b, dayPlanElementId=ab683a25-633e-419e-89b6-4aef7829d4f6]].

       at org.hibernate.event.internal.EventCache.put(EventCache.java:192)

       at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:285)

       at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)


I debugeg the EventCache, seaching for problem solutions, just check implemented equals() and hashCode() method - without success:-(( Notice, i use for equals() method the Id. For instance the composite id of DayPlanElementEntry. That id is used by hashCode(). I just not understand the error message, thrice times will be output the same properties - the ids!

I found the test class for event cache, but now i don't understand the following use case test:

    @Test
public void testCopyAssociatedWith2ExistingEntities() {
session.getTransaction().begin();
Simple entity1 = new Simple( 1 );
session.persist( entity1 );
Simple copy1 = new Simple( 1 );
cache.put(entity1, copy1);
Simple entity2 = new Simple( 2 );
session.persist( entity2 );
Simple copy2 = new Simple( 2 );
cache.put( entity2, copy2 );
session.flush();

try {
cache.put( entity1, copy2 );
fail( "should have thrown IllegalStateException");
}
catch( IllegalStateException ex ) {
// expected
assertTrue( ex.getMessage().startsWith( "Error occurred while storing entity [org.hibernate.event.internal.EventCacheTest$Simple#1]." ) );
}
session.getTransaction().rollback();
}

....

    @Entity
private static class Simple {
@Id
private int value;

public Simple(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}
}


We have the Simple entity entity1 with Id=1, Then we persist it. Then we create the Simple entity copy1 and put into event cache it as value. As key the persisted entity*1* used. Then we create a Simple entity*2* with Id=2 , persist it, and then create the copy*2* entity with id 2 and put it as value into event cache. As key the persisted entity2 used. Why such situation is wrong and why is expected, that IllegalStateException must be thrown??

Thanks for your help!

解决方案

For Hibernate version 4.3.4 and around, there is a fix out. Just add this artifact:

<dependency>
    <groupId>com.intersult</groupId>
    <artifactId>jpa-fix</artifactId>
    <version>1.1</version>
</dependency>

This fix replaces the MergeEventListener with such one, that handles this kind of merge.

这篇关于休眠:实体副本已被分配给不同的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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