Difference between Transient, Persistent, and Detached Objects in Hibernate

In Hibernate framework, an entity can be in three states, transient, persistent, and detached. When an object is in transient state, it is commonly refereed as transient object, similarly if it is in persistence and detached state, it is known as persistent and detached object. When an entity is first created using the new operator e.g. new User() and not associated with Hibernate session e.g. you haven't called session.save(user) method then it is known as transient object. At this stage, Hibernate doesn't know anything about this object and the object doesn't have any representation in database e.g. a corresponding row in the User table. Hibernate will not run any SQL query to reflect any changes on this object. You can move this object into persistent state by associating it with an hibernate session e.g. by calling save() or saveOrUpdate() method from an hibernate Session.

When an entity object moved to Persistence state it become responsibility of Hibernate. Now if you make any change on entity object e.g. change any attribute like user.setName("Mike"), Hibernate will automatically run the update queries to persist the change into database. A persistence object has corresponding representation on the database.

When you close the hibernate session or call the evict() method then the object moves to the detached state. In this state, hibernate doesn't track the object but you can re-attach a detached object to Hibernate session by calling the update() or saveOrUpdate(), or merge() method. Once reattached, the detached object will move to Persistent state.




Difference between Transient vs Persistent vs Detached Object in Hibernate

'This is also one of the frequently asked Hibernate Interview questions and even though, both Transient and Detached object is not associated with hibernate session, there is a key difference between them. First, detached object was associated with Hibernate session in past and it has representation in database, on the other hand, Transient object is never associated with hibernate and it doesn't have any representation in database.

But, both can be moved to Persistent state by associating them with session e.g. you can move an entity from transient to persistent state by calling Session.save() method. Similarly, you can move a detached entity to Persistent state by calling Session.update() or Session.saveOrUpdate(), or Session.merge() methods.

let's see some more difference between transient, persistent, and detached objects in Hibernate in a point based format for easier understanding:




Database Representation
The main difference between transient, persistent, and detached object comes from representation in database. When an entity is first created, it goes to transient state and this time it doesn't have a representation in database i.e. there will be no row corresponding to this object in Entity table. On the other hand, both Persistent and Detached objects has corresponding representation in database.


Association with Hibernate
Another key difference between transient, persistent, and detached objects comes from the fact that whether they are associated with session or not. The transient object is not associated with session, hibernate knows nothing about them. Similarly detached object is also not associated with session, but Persistent object is associated with session.

Hence any changes in the Persistent object will reflect in database because Hibernate will automatically run update queries to save changes on Persistent object. See Java Persistence with Hibernate for more details.

Difference between Transient, Persistent, and Detached state in Hibernate



Impact of GC
Both transient and detached objects are eligible for garbage collection because they are not associated with session, when GC will run they can be collected, but persistent object is not eligible to garbage collection until session is open because Hibernate Session object keep a reference of Persistent object.


State transition
When an entity is first created in application using  the new() operator, it remains in transient state. It can move to Persistent state when you associate it with a session by calling Session.save() method. When you close() the session or evict() that object from session, it moves to detached state. You can again move a detached object to Persistent state by calling Session.update() or Session.saveOrUpdate() method.

Here is a nice Hibernate state diagram which shows how state transition happens in Hibernate by calling different methods in an hibernate entity objects life-cycle:

Difference between Transient, Persistent, and Detached Objects in Hibernate

You can see that when a new object is created it goes to Transient state and then when you call save() or saveOrUpdate() method it goes to Persistent state, and when you call the evict(), clear(), or close() method, it goes to Detached state. Similarly, when you call get() or load() method the object goes to Persistent state because it has representation in database.


That's all about difference between transient, persistent, and detached object in Hibernate. As an Hibernate developer you must know how an object's life-cycle works in Hibernate i.e. when an object goes to transient and detached state, how it goes to persistent state and what does Hibernate do when an object is in persistent state i.e. automatically saving objects state into database by running SQL queries.


Further Learning
Introduction To Hibernate
Spring and Hibernate for Beginners
Hibernate Interview Questions Preparation Course


Other Hibernate Articles and Interview Questions you may like
  • Difference between First and Second level cache in Hibernate? (answer)
  • Difference between get() and load() method in Hibernate? (answer)
  • 5 Spring and Hibernate Training Courses for Java developers (courses)
  • 2 Books to Learn Hibernate in 2017 (books)
  • 5 Books to Learn Spring Framework in 2017 (books)
  • Why Hibernate Entity class should not be final in Java? (answer)
  • 10 Hibernate Questions from Java Interviews (list)

Thanks for reading this article, if you like this article and interview question then please share with your friends and colleagues. If you have any question or feedback then please drop a comment. If you want to learn more about such key fundamentals of Hibernate, then I also suggest you reading Java Persistent with Hibernate, 2nd Edition  by Christian Bauer and Gavin King for more in-depth information on this topic.

No comments :

Post a Comment