What are detached, persistent and transient objects in hibernate?
What are detached, persistent and transient objects in hibernate? Please explain with an example.
Solution 1:
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.
Solution 2:
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
Solution 3:
Let me explain in Garbage collector point of view also.
There are 3 Object states of hibernate (or) Object Scope of hibernate-
- Transient state
- persistent state
- 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)
Solution 4:
Given the following entity:
@Entity
public class City {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
// other fields and methods.
}
From Hibernate 5.2 documentation (I've also included the removed
state):
transient
the entity has just been instantiated and is not associated with a persistence context. It has no persistent representation in the database and typically no identifier value has been assigned (unless the assigned generator was used).
City city = new City();
managed, or persistent
the entity has an associated identifier and is associated with a persistence context. It may or may not physically exist in the database yet.
// city will be in a managed/persistent state and any changes to it, will be tracked by hibernate
// and reflected to the database when the persistence context is flushed.
session.save(city);
detached
the entity has an associated identifier, but is no longer associated with a persistence context (usually because the persistence context was closed or the instance was evicted from the context)
// city is in a detached state, Hibernate is no longer aware of the entity
session.evict(city)
removed
the entity has an associated identifier and is associated with a persistence context, however it is scheduled for removal from the database.
session.remove(city);
Note:
Hibernate API offers couples of methods to switch between entity states, and I think it's worth exploring a Hibernate Session class.