Sunday, October 6, 2013

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

save method:- it is used to insert the newly created object in datastore.(Basically identifier value will be 0 for this). Like i create a new customer and call save operation , it will persist it in datastore and generate the identifier.
So calling save again will result in another row in the db.

update method:- it is used to update the already persistent object in datastore.(Basically identifier value will be some non zero value for this). Like i load a new customer and call update operation after update of some field value, it will update it in datastore .As per my understanding it should fail with some exception because as per api update is for detached object.

update updates an object in the session. So if the object is in the session it will update. If the object is not in the session, you should call merge. I believe calling update for a detached instance will result in an exception.
saveorupdate method:- it will call either of above based on unsaved-value checks. so if we have persistent customer object . Now if we update last name of his and create a new account also, then saveorupdate will take care of it. 
Merge method :- it will like update but here if persistent object with the same identifier is already there in session it will update the detached object values in persistent object and save it. Right? But if If there is no persistent instance currently associated with the session, this will load the persistent object from datastore and then update the value of detached object in loaded persistent object and then update it.

 above chek again pls

Rather than sing update if we use, merge this can be solved.

No comments:

Post a Comment