IllegalStateException:存储实体< entity>时发生错误实体拷贝<实体>已被分配给不同的实体< entity_copy> [英] IllegalStateException: Error occurred while storing entity <entity> An entity copy <entity> was already assigned to a different entity <entity_copy>
问题描述
问题:
//在第一个会话中,我做
session1.save(entity);
session1.getTransaction()。commit();
session1.close();
//在第二个会话中(客户端响应之后),我找回了实体的序列化副本
entityCopy = deserialize(jsonString);
entityCopy.setEntityDetail(newDetail); // 1
session2.merge(entityCopy); //例外!
如果注释字符串1,一切正常!
异常::
$ b IllegalStateException :实体#4700实体副本#4700已被分配给不同的实体@ 2e7b134a
问题:
- 我的感觉出了什么问题?
- 据我所知,merge()被实现了这些情况下,当我们可以有实体拷贝缓存。
- 如果重要实体 - > EntityDetail 与 lazy,orphanRemoval = true链接,则一对一 关系
- 我重写了equals()和hashCode()方法。
- What is wrong in my sitation?
- As I understand, merge() operation was implemented for those cases, when we alreadty have the entity copy in cache. Am I wrong?
- If it is important Entity -> EntityDetail are linked with lazy, orphanRemoval = true, one-2-one relationship
- I overrided equals() and hashCode() methods.
我以下面的方式解决了这个问题:反序列化的实体,然后再对其进行一些更改。 (唯一的变化是2字符串):
//在第一个会话中,我做
session1.save (实体);
session1.getTransaction()。commit();
session1.close();
//在第二个会话中(客户端响应之后),我找回了实体的序列化副本
entityCopy = deserialize(jsonString);
entityCopy =(Entity)session.merge(entityCopy); // 2
entityCopy.setEntityDetail(newDetail);
session2.merge(entityCopy); // all works fine
After a lot of googling I didn't find answer to my question, except downgrade hibernate version. But I faced this situation at similar post dated as 2003 year.
What the problem:
//in the first session I do
session1.save(entity);
session1.getTransaction().commit();
session1.close();
//in the second session (after client response), I get back the serialized copy of the entity
entityCopy = deserialize(jsonString);
entityCopy.setEntityDetail(newDetail); //1
session2.merge(entityCopy); //EXCEPTION!
If comment string 1, all works fine!
Exception:
IllegalStateException: Error occurred while storing entity #4700 An entity copy #4700 was already assigned to a different entity @2e7b134a
Questions:
PS
I solve this problem in the next way: it is necessary to merge deserialized entity before you proceed some changes to it. (the only change is in the 2 string):
//in the first session I do
session1.save(entity);
session1.getTransaction().commit();
session1.close();
//in the second session (after client response), I get back the serialized copy of the entity
entityCopy = deserialize(jsonString);
entityCopy = (Entity) session.merge(entityCopy); //2
entityCopy.setEntityDetail(newDetail);
session2.merge(entityCopy); //all works fine
这篇关于IllegalStateException:存储实体< entity>时发生错误实体拷贝<实体>已被分配给不同的实体< entity_copy>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!