开发者

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

开发者 https://www.devze.com 2023-01-14 19:05 出处:网络
This question already has an answer here: 开发者_JS百科 Closed 10 years ago. Possible Duplicate: Hibernate: different object with the same identifier value was already associated with the ses
This question already has an answer here: 开发者_JS百科 Closed 10 years ago.

Possible Duplicate:

Hibernate: different object with the same identifier value was already associated with the session

I have got almost the same problem like that user.

In my situation I load one entity from db, I convert this entity into a DataTransferObject, then I want to edit one attribute, after that I convert it back into an entityObject, then I update that entity and hibernate throws following error:

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

Apparently, the problem is that the object I retrieve from db has the same id as the one I want to update (like it should be) BUT those are not the same objects!

How to manage that? Thank you for help...


Your problem is that the object you previously loaded still exists in your hibernate session. I see two ways to cope with that.

1.) tell the hibernate session to merge your modified object with the one in the session

session.merge(object)

2.) kick the old object out of the session before writing the updated object to the session. session.clear() might work.


This is a very common problem with Hibernate. Even if you delete it from the session, the object will stay in Hibernate PersistanceContext and you will have the same problem. The problem is also coming from the fact that session.contains uses object equality, and not equals() to compare the object...

The story is the following: you have object A and object B,which are the same logical object, but two different objects in your java heap. and you have long time ago did something with object A in the session.

Now if you do session.delete(B) you will get the exception, because you are trying to delete an object which has the same primary key as A, but is not A.

The solution is simple:

Object findAAgain=session.merge(B);
session.delete(findAAgain);

the merge returns the instance of the object you have in the session first.


choose either one:

1 - you can close the session after loading the entity and open new session for update

2 - instead of creating new Entity for update, use the old one and edit it.

3 - detach your first entity from session - session.evict(myEntity)


Beyond just calling clear() on the session, it sounds like you might have a problem with the way you are using Hibernate:

Apparently, the problem is that the object I retrieve from db has the same id as the one I want to update (like it should be) BUT those are not the same objects!

Do you mean to say that you have two distinct entities which have the same ID? If so, then you should find a different field that uniquely identifies different entities.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号