Sponsored Links

Ad by Google
In Hibernate interview most of the time this question asked by interviewer by asking different statements for example, What are the states of Hibernate instances? or How many instances of Hibernate object? or How many types of object in Hibernate?

Well answer of these question is three. Hibernate has 3 different types of object states, all three states are related to Hibernate persistence context and these are listed below.
  1. transient
  2. persistent
  3. detached
An object is a transient object, if and only if object is just instantiated using new operator and it is not associated with Hibernate session. There is no any persistence representation of the this object in the database and no any identifier value is associated with the object.
For example,

// Here user is transient object.
User user = new User();

An object is in persistent state if it has the representation in the database with identifier value. Persistent object has the scope in Session. Persistent object may be just inserted object in the database with identifier value, and a row value representation in the database.
For example,

User user = new User();
Long savedID = (Long)session.save(user);

The object was associated with persistence context, but the context was closed for now, or the object was serialized to another process. A detached object can be reattached to a new session at any point of time.

Reference 1

Sponsored Links


Post a Comment