Hibernate Error: org.hibernate.NonUniqueObjectExce

2019-01-01 10:50发布

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

session.save(userObj);

I am getting the following error:

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]

I am creating the session using

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

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.

Any suggestions?

30条回答
明月照影归
2楼-- · 2019-01-01 11:08

@GeneratedValue(strategy=GenerationType.IDENTITY), adding this annotation to the primary key property in your entity bean should solve this issue.

查看更多
宁负流年不负卿
3楼-- · 2019-01-01 11:10

before the position where repetitive objects begin , you should close the session and then you should start a new session

session.close();      
session = HibernateUtil.getSessionFactory().openSession();

so in this way in one session there is not more than one entities that have the same identifier.

查看更多
爱死公子算了
4楼-- · 2019-01-01 11:11

You can check your Cascade Settings. The Cascade settings on your models could be causing this. I removed Cascade Settings (Essentially not allowing Cascade Inserts/Updates) and this solved my problem

查看更多
唯独是你
5楼-- · 2019-01-01 11:12

This can happen when you have used same session object for read & write. How? Say you have created one session. You read a record from employee table with primary key Emp_id=101 Now You have modified the record in Java. And you are going to save the Employee record in database. we have not closed session anywhere here. As the object that was read also persist in the session. It conflicts with the object that we wish to write. Hence this error comes.

查看更多
素衣白纱
6楼-- · 2019-01-01 11:12

I ran into this problem by:

  1. Deleting an object (using HQL)
  2. Immediately storing a new object with the same id

I resolved it by flushing the results after the delete, and clearing the cache before saving the new object

String delQuery = "DELETE FROM OasisNode";
session.createQuery( delQuery ).executeUpdate();
session.flush();
session.clear();
查看更多
冷夜・残月
7楼-- · 2019-01-01 11:12

Check if you forgot to put @GenerateValue for @Id column. I had same problem with many to many relationship between Movie and Genre. The program threw Hibernate Error: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session error. I found out later that I just have to make sure you have @GenerateValue to the GenreId get method.

查看更多
登录 后发表回答