Monday, August 1, 2016

When update() method has to call in Hibernate and what is the difference between merge and update?


Transient state?
Transient state means, Object is neither associated with Session nor present in Database. When you don’t have record in the database, then you will not be able to update the record. So when the object is transient state you will not call update() method.

Persistent state?
Persistent state means, Object is associated with Session and also present in Database. That means object is synchronized with database. So if you do modifications to the object, the changes will be updated in the database and vice versa. So when the object is persistent state you will not call update() method.

Detached state?
Detached state means, Object is not associated with Session, but present in Database. In this state, when you do any modification to the object the changes will not be updated in database. We should call update() method ,so that it the data will be updated in the database. So whenever you do modification to the object which is in detached state, you need to call update() method , so that changes will be updated in database.

So we need call the update method when the object in Detached state.

merge(): It merge the detached object data into persistent data.

What is the difference between merge and update? 

update (): Use update(), when a session does not contain the persistent instance with the same identifier
merge (): Use merge(), Irrespective of the state of a session, if you need to save the modifications at any given time.

No comments:

Post a Comment