Friday, July 9, 2010

Hibernate persistence lifecycle

Target Audience: Beginners
Hibernate has three states respect to its persistence lifecycle which are transient, persistent and detached. Hibernate persistent manager has some callback methods which involves in those persistent states. In a lifecycle, the object can transition from transient to persistent to detached states.

Transient objects
When an object is instantiated (with new operator), it’s not persistent immediately. It’s first goes to the transient state which is in memory. In this state it’s not related to any database table row. When that object is de-referenced it loses its state and goes to detached state which will be eligible to garbage collected.
The objects that are in transient state are not considered as transactional objects. Any modification made on those object can not be part of transaction. If a transient object need to be persist or in other words if an object need to changes its state from transient to persistent, then the save () method should be called to persistent manager or that object should refer to one which was created from already persisted.

Persistent objects
Persistent instance is which has a valid database entry set with a primary key identifier. In other words, it should refer a valid database table row.
Persistent object can be instantiated with a call of save () method of the persistent manager then it’s associated to that persistent manager. Or alternatively it can be instantiated with a persistent object which is already associated with that persistent manager.
Persistent object participated in a transaction will be synchronized with the database when the transaction manager commits the transaction which executes the SQL query. Some times, the synchronization will happen before the execution of the queries just to make sure that the queries aware of the changes done during the previous transaction.
When the transaction ends, all the persistent object will not be updated to the database. Hibernate checks the objects and finds which are modified then only it’ll update the database. This process is called dirty checking. Dirty objects are which has had modified and not synchronized with database. This process gains performance with some databases and loose performance with some databases. So hibernate allows user to set this with dynamic-update="true" in class mapping so it would generate SQL dynamically to update only the columns that are modified.
When the persistent manager class the delete () method the data will be removed from database and the persistent object will move fro persistent state to transient sate.

Detached objects
Detached object are which no longer guaranteed to be synchronized with database state.
The persistence instances will still exist even after the transaction completes. These instances will be detached when the close () the Session. But still those object holds the data , Hibernate can detain this objects out side of the transaction manager and persistent manager, with a new transaction manager and with a new persistent manager.
Persistent objects can not be detached explicitly. The evict () method of Session can be used to detach the object from session cache. All persistent objects will be detached when close () the session or the objects are serialized.

No comments:

Post a Comment