Thursday, June 11, 2009

Hibernate Persistence Lifecycle

Hibernate persistence lifecycle has 3 states.

1. Transient
2. Persistent
3. Detached

Transient State

When create object using new operator, it is immediately goes to transient state. In this state the object is not connected with database and it is in memory. In this state if object is dereference, then it is goes to detached mode and garbage collector will clean it. To move object to persistent state you have to save this. If you create object from persist data, then the object is not in Transient state and it is in persistent state since the object is connected with database.

Persistent State

When the object is referred to the valid database row, the object is in persistent state. When we call the delete() method, the data in database will remove and object will move to transient state.

Detached State

The persistent objects are holding data even after the transaction is completed. These objects will be detached from database when we close the session. You can detached object by calling evict() method.

No comments:

Post a Comment