Hibernate: Different Object with the Same Identifier Value Was Already Associated with the Session

Hibernate Error: a different object with the same identifier value was already associated with the 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.

Hibernate Error: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session

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

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.

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?

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.

Example

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

hibernate NonUniqueObjectException: a different object with the same identifier value was already associated with the session:

public String getAccountsDetails(AccountDetails accountDetails) {
System.out.println("accountDetails.accoundId-->"+accountDetails.getAccountsId());
Session session = sessionFactory.openSession();
Query query = session.createQuery("from QBAccounts qba where qba.accountsId=:accId");
List<AccountDetails> queryList = query.setParameter("accId", accountDetails.getAccountsId()).list();
session.close();
Session session2 = sessionFactory.openSession();
// session.saveOrUpdate(accountDetails);
try{
if(queryList.size()>0){
session2.beginTransaction();
/*
* comment below line-50,51 to use update instead of merge, and uncomment line 53
*/
AccountDetails acDet = (AccountDetails) session2.get(AccountDetails.class, new Long(1));//line 50
session2.merge(accountDetails);//line 51
System.out.println("acDet-->"+acDet+" --accountDetails-> "+accountDetails);//line 52
// session2.update(accountDetails);//line 53
}else{
session2.beginTransaction();
session2.save(accountDetails);
}
}catch(DIRException e){
session2.getTransaction().rollback();
System.out.println("Getting Exception : " + e.getLocalizedMessage());
}finally{
session2.getTransaction().commit();
session2.close();
}
return "Successfully data updated into table";
}

Removed @GeneratedValue from my dto, since value was coming from UI.

Here is the good article, how to use MERGE/UPDATE

Hibernate Spring JPA Session: A different object with the same identifier value was already associated with the session

You should not update entities in that way, this only fits for creating new entities.

productRepo.save(prod)

Correct way is to first load entity by id (this would attach object to session), update it and then save

Product existing = productRepo.findById(prod.getId());
existing.getStock().setCount(+prod.getStock().getCount());
productRepo.save(existing);

But in your code you receive "unmanaged" (that is not attached to hibernate session) object with existing ID (!), update it and save. Of course it would give an error



Related Topics



Leave a reply



Submit