When update() method has to call?

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

Example on Session update method.

In this example we will update the employee record which is there in the database.

Before executing the EmployeeUpdateExample.java in database

Execute EmployeeUpdateExample.java

After executing the EmployeeUpdateExample.java in database


Posted in hibernate and tagged , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.