Hibernate Object States Revealed

Hibernate connects an object (in Java or .NET) with its persistent state in database. While working with hibernate, it is very important to understand how an object instance in java gets connected to a row in table and how it traverses through different states.

There are three important life cycle states of an object.

– Transient

– Persistent

– Detached

Each of these three states is clearly identifiable in an object’s life cycle. Meaning of these states can be broadly defined as follows:

Transient:
Transient state objects are not currently attached to any session. This definition results in two scenarios – first where the objects are created by application but not connected to a session, and second the objects are created by a closed session. But the resultant meaning is simple – the transient object is yet to be connected with a row (s) in database. This does not mean that there will be no instance of same object in database. It is possible that there exists a row in database exactly mapping with this transient object and when we try to persist this object in session/db, the database throws error of duplicity. E.g. we create a customer object with first name A and last name B. Database customer table has a composite primary key with first name and last name. This table already contains a row with these attribute values. Now when we try to save our object in database, we end up in error. But there may not be an error when we try to update the existing object, as it will be clearly identified using the primary key.
Generally, the transient state can be identified by null (no) value of primary key attribute in the object.

Persistent:
 As name means, this state represents existence of object in permanent storage. There will be connection between the object in memory and in database through the identifier. Any change in either of these two will be reflected in other (when transaction is committed).
 Load/save/saveorupdate operations result in retrieval of a persisted object from database. This does not mean that all attributes in object will be populated with database values, but the primary keys will be fetched for sure. Retrieval of remaining attributes depends on the mapping attribute to define whether the fetch is eager or lazy. Eager fetch will populate all attributes but lazy loading will retrieve only that data which is necessary to retrieve remaining attributes. This retrieval will happen when application tries to access remaining attributes. Lazy loading configuration is connected with performance of loading operation.
 Persistent state is dependent on session object. First, session has to be open (unclosed), and second, the object has to be connected to the session. If either of these two is not true, then the object moves into either transient state or detached stage.

Detached:
This state arises when a persistent state object is not connected to a session object. No connection may be because the session itself is closed or the object is moved out of session. In an application, this scenario arises when object traverses through different layers of the application. To attaché an object back to a session, it may need loading of the object from database using primary key available in the detached object. It is possible that the detached object is modified and when we try to connect it back to session and subsequently the persisted state, we might need to take a call whether we really want to update the database object with modified values.

Knowledge of these states is important because we will have to define course of action based on current state of object. Below are few scenarios explaining the importance.

Detached object to save: If we perform save operation on a detached object with sequence generated primary key, and there is no other unique constraint on table, then there is possibility of duplicate data being entered.

Transient or Detached: When an object is disconnected form session it is detached, but once detached the object does not carry any connection with database except the primary key/identifier. If, during application processing, we modify the primary key, then there is no possibility of representing same row in database and that object may be treated as transient if the identifier is nullified.

Native Sql Usage: One can choose native sql over hibernate mapping for better performance in complex queries. Objects populated with this data are usually detached. Here the completion of query execution, finishes the database connectivity of row.

O/R mapping considerations:
The attributes which play important role with state of object are – lazy and cascade.

lazy
As discussed above, lazy loading is preferred to improve performance in case of very large collections. Specifying lazy equal to true, loads only integrity attributes, and rest of the attributes are loaded if that other attributes are accessed by calling program. If lazy loaded object is detached then it may have few attributes not populated and the application may end up with-un availability of data.

cascade
This attribute defines how a CRUD (create, read, update or delete) operation propagates on child of an object. For hibernate handling purpose, there may be a time, where parent and children are in different states.

  • Share/Bookmark
Hibernate, Tech Notes

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

One Response to “Hibernate Object States Revealed”

Leave Comment

(required)

(required)