3

经过大量谷歌搜索后,除了降级休眠版本外,我没有找到问题的答案。但是我在 2003 年的类似职位上遇到了这种情况。

什么问题:

//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!

如果注释字符串 1,一切正常!

例外:

IllegalStateException存储实体 #4700 时发生错误实体副本 #4700 已分配给不同的实体 @2e7b134a

问题:

  1. 我的位置有什么问题?
  2. 据我了解,当我们已经在缓存中拥有实体副本时,为这些情况实施了 merge() 操作。我错了吗?

附言

  1. 如果是重要的Entity -> EntityDetail是用lazy 链接的,orphanRemoval = true,一对二的关系
  2. 我重写了 equals() 和 hashCode() 方法。
4

1 回答 1

1

我通过以下方式解决了这个问题:在对它进行一些更改之前,有必要合并反序列化的实体。(唯一的变化是在 2 字符串):

//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
于 2013-08-13T06:14:16.630 回答