休眠错误:具有相同标识符值的不同对象已与会话相关联 [英] Hibernate Error: a different object with the same identifier value was already associated with the session

查看:293
本文介绍了休眠错误:具有相同标识符值的不同对象已与会话相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个配置中实际上有一些对象(真正的数据模型有点复杂):


  • (B有 inverse =true

  • B与多对一的关系C.(我有 cascade 设为save-update

  • C是一种类型/类别表。



另外,我应该提到主键是由数据库生成的保存。

有了我的数据,我有时会遇到问题,其中A有一组不同的B对象,而这些B对象指向相同的C对象。



当我调用 session.saveOrUpdate(myAObject)时,出现hibernate错误:a具有相同标识符值的不同对象已经与会话相关联:C。我知道hibernate不能在同一个会话中插入/更新/删除同一个对象两次,但是有没有办法解决这个问题?这似乎并不是那种罕见的情况。



在我研究这个问题的过程中,我看到人们建议使用 session.merge(),但是当我这样做时,任何冲突对象都会作为空白对象插入到数据库中,并将所有值设置为null。很明显,这不是我们想要的。



我忘记提及的另一件事是(对于超出我的控制的架构原因),每个读或写需要在一个单独的会话中完成。

解决方案

很可能是因为B对象没有引用相同的Java C对象实例。它们指的是数据库中的同一行(即相同的主键),但它们是不同的副本。因此,发生的事情是,管理实体的Hibernate会话将跟踪哪个Java对象与具有相同主键的行相对应。

p>

一种选择是确保引用同一行的对象B的实体实际上是指C的同一对象实例。或者关闭该成员的级联变量。当B坚持时,这种方式C不是。不过你必须手动保存C.如果C是一个类型/类别表,那么这可能是有道理的。


I essentially have some objects in this configuration (the real data model is a bit more complex):

  • A has a many-to-many relationship with B. (B has inverse="true")
  • B has a many-to-one relationship with C. (I have cascade set to "save-update")
  • C is a kind of type/category table.

Also, I should probably mention that the primary keys are generated by the database on save.

With my data, I sometimes run into problems where A has a set of different B objects, and these B objects refer to the same C object.

When I call session.saveOrUpdate(myAObject), I get a hibernate error saying: "a different object with the same identifier value was already associated with the session: C". I know that hibernate can't insert/update/delete the same object twice in the same session, but is there some way around this? This doesn't seem like it would be that uncommon of a situation.

During my research of this problem, I have seen folks suggest the use of session.merge(), but when I do that, any "conflicting" objects get inserted into the database as blank objects with all values set to null. Clearly that isn't what we want.

[Edit] Another thing I forgot to mention is that (for architectural reasons beyond my control), each read or write needs to be done in a separate session.

解决方案

Most probably its because the B objects are not referring to the same Java C object instance. They are referring to the same row in the database (i.e. the same primary key) but they're different copies of it.

So what is happening is that the Hibernate session, which is managing the entities would be keeping track of which Java object corresponds to the row with the same primary key.

One option would be to make sure that the Entities of objects B that refer to the same row are actually referring to the same object instance of C. Alternatively turn off cascading for that member variable. This way when B is persisted C is not. You will have to save C manually separately though. If C is a type/category table, then it probably makes sense to be that way.

这篇关于休眠错误:具有相同标识符值的不同对象已与会话相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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