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

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

问题描述

我有两个用户对象,当我尝试使用

I have two user Objects and while I try to save the object using

session.save(userObj);

我收到以下错误:

Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
[com.pojo.rtrequests.User#com.pojo.rtrequests.User@d079b40b]

我正在使用

BaseHibernateDAO dao = new BaseHibernateDAO();          

rtsession = dao.getSession(userData.getRegion(),
                           BaseHibernateDAO.RTREQUESTS_DATABASE_NAME);

rttrans = rtsession.beginTransaction();
rttrans.begin();

rtsession.save(userObj1);
rtsession.save(userObj2);

rtsession.flush();
rttrans.commit();

rtsession.close(); // in finally block

我也尝试过在保存之前执行 session.clear(),仍然没有运气.

I also tried doing the session.clear() before saving, still no luck.

这是我第一次在用户请求到来时获取会话对象,所以我明白为什么说会话中存在该对象.

This is for the first I am getting the session object when a user request comes, so I am getting why is saying that object is present in session.

有什么建议吗?

推荐答案

这个错误我遇到过很多次,而且很难追查...

I have had this error many times and it can be quite hard to track down...

基本上,hibernate 的意思是你有两个对象,它们具有相同的标识符(相同的主键),但它们不是同一个对象.

Basically, what hibernate is saying is that you have two objects which have the same identifier (same primary key) but they are not the same object.

我建议你分解你的代码,即注释掉一些直到错误消失,然后把代码放回去,直到它回来你应该找到错误.

I would suggest you break down your code, i.e. comment out bits until the error goes away and then put the code back until it comes back and you should find the error.

它最常通过级联保存发生,其中对象 A 和 B 之间存在级联保存,但对象 B 已经与会话相关联,但与 A 上的实例不在 B 的同一实例上.

It most often happens via cascading saves where there is a cascade save between object A and B, but object B has already been associated with the session but is not on the same instance of B as the one on A.

您使用的是什么主键生成器?

What primary key generator are you using?

我问的原因是这个错误与您如何告诉 hibernate 确定对象的持久状态(即对象是否持久)有关.该错误可能发生,因为 hibernate 试图持久化一个已经持久化的对象.事实上,如果您使用 save hibernate 将尝试持久化该对象,并且可能已经有一个与会话关联的具有相同主键的对象.

The reason I ask is this error is related to how you're telling hibernate to ascertain the persistent state of an object (i.e. whether an object is persistent or not). The error could be happening because hibernate is trying to persist an object that is already persistent. In fact, if you use save hibernate will try and persist that object, and maybe there is already an object with that same primary key associated with the session.

示例

假设您有一个基于主键组合(第 1 列和第 2 列)的 10 行表的休眠类对象.现在,您已经在某个时间点从表中删除了 5 行.现在,如果您尝试再次添加相同的 10 行,而 hibernate 尝试将对象持久保存在数据库中,则会添加已删除的 5 行而不会出错.现在已经存在的剩余 5 行将抛出此异常.

Assuming you have a hibernate class object for a table with 10 rows based on a primary key combination (column 1 and column 2). Now, you have removed 5 rows from the table at some point of time. Now, if you try to add the same 10 rows again, while hibernate tries to persist the objects in database, 5 rows which were already removed will be added without errors. Now the remaining 5 rows which are already existing, will throw this exception.

因此,最简单的方法是检查您是否更新/删除了表中的任何值,该值是某事物的一部分,然后您是否尝试再次插入相同的对象

So the easy approach would be checking if you have updated/removed any value in a table which is part of something and later are you trying to insert the same objects again

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

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