IllegalStateException:存储实体 时发生错误 实体副本 已分配给不同的实体

2023-12-24

经过大量谷歌搜索后,除了降级休眠版本之外,我没有找到问题的答案。但我在 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,则一切正常!

例外:

非法状态异常: 存储实体 #4700 时发生错误实体副本 #4700 已分配给不同的实体 @2e7b134a

问题:

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

PS

  1. 如果很重要的话实体 -> 实体详细信息懒惰,orphanRemoval = true,一二一关系
  2. 我重写了 equals() 和 hashCode() 方法。

我用下一种方式解决这个问题:有必要在对反序列化实体进行一些更改之前合并它。 (唯一的变化是在 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IllegalStateException:存储实体 时发生错误 实体副本 已分配给不同的实体 的相关文章

随机推荐