What are detached, persistent and transient objects in hibernate?

Let me explain in Garbage collector point of view also.

There are 3 Object states of hibernate (or) Object Scope of hibernate-

  1. Transient state
  2. persistent state
  3. detached state

It is better to understand with a code example-

Let us consider a POJO class as Student Object->

Student student = new Student(); 

Now, this student object is at transient state.


When we attache this POJO object to hibernate session->

session.save(student);

Now this POJO object is at persistent state.

(Garbage collector point of view- GC cannot wipe-out Any object which is in the persistent state. Soo we can say that persistent state is like temporary storage for POJO objects)


If we perform->

session.beginTransaction.commit();

then the POJO object is at Permanent or Database storage state

(Garbage collector point of view- GC cannot wipe-out this object because this POJO object is now outside the scope of JVM and stored in the form table inside a database.Soo we can say that this Database storage state is like permanent storage for POJO objects)


If we perform->

session.evict(student); 

then POJO object is evicted or removed back from the persistent state to detached state.Soo this state of POJO object is detached state.

(Garbage collector point of view- GC can easily wipe-out the detached state POJO object from JVM)


Object in hibernate has following states:

Transient - Objects instantiated using the new operator are called transient objects.

An object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application does not hold a reference anymore.

Persistent - An object that has a database identity associated with it is called a persistent object.

A persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded; however, it is by definition in the scope of a Session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes.

Detached - A detached instance is an object that has been persistent, but its Session has been closed.

A detached instance can be reattached to a new Session at a later point in time, making it persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e., a unit of work from the point of view of the user.

http://webiwip.com/interview-questions-answers/hibernate-interview-questions/32012


A new instance of a persistent class which is not associated with a Session, has no representation in the database and no identifier value is considered transient by Hibernate:

Person person = new Person();
person.setName("Foobar");
// person is in a transient state

A persistent instance has a representation in the database, an identifier value and is associated with a Session. You can make a transient instance persistent by associating it with a Session:

Long id = (Long) session.save(person);
// person is now in a persistent state

Now, if we close the Hibernate Session, the persistent instance will become a detached instance: it isn't attached to a Session anymore (but can still be modified and reattached to a new Session later though).

All this is clearly explained in the whole Chapter 10. Working with objects of the Hibernate documentation that I'm only paraphrasing above. Definitely, a must-read.