Wednesday, 29 June 2016

What are the states of object in hibernate?

States of object in hibernate

States of object (instance) in hibernate

Transient: The object is in transient state if it is just created but has no primary key (identifier) and not associated with session i.e. doesn’t represent any row of the database.

Persistent: It represent one row of the database and always associated with some unique hibernate session.

The object is in persistent state if session is open, and you just saved the instance in the database or retrieved the instance from the database.

Changes to persistent objects are tracked by hibernate and are saved into database when commit call happen.

Detached: Detached objects are those who were once persistent in past (object is in detached state if session is closed) and now they are no longer persistent.

After detached state, object comes to persistent state if we call lock() or update() or saveOrUpdate() method (reattach object to hibernate session).

If we want to move an object from persistent to detached state, we need to do either closing that session or need to clear the cache of the session.

If we want to move an object from persistent state into transient state then we need to delete that object permanently from the database.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...