In the Hibernate framework, an entity can be in three states, transient, persistent, and detached. When an object is in a transient state, it is commonly referred to as a transient object, similarly, if it is in persistence and detached state, it is known as a persistent and detached object. When an entity is first created using the new operator like new User() and not associated with Hibernate session like you haven't called session.save(user) method then it is known as a transient object. At this stage, Hibernate doesn't know anything about this object and the object doesn't have any representation in the database like 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 a persistent state by associating it with a hibernate session e.g. by calling save() or saveOrUpdate() method from a hibernate Session.
When an entity object moved to Persistence state it becomes the responsibility of Hibernate. Now if you make any change on entity object like change any attribute like the user.setName("Mike"), Hibernate will automatically run the update queries to persist the change into the database. A persistence object has a 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 a Persistent state..
First, the detached object was associated with Hibernate session in past and it has representation in the database, on the other hand, the Transient object is never associated with hibernate and it doesn't have any representation in the 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:
Hence any changes in the Persistent object will reflect in the database because Hibernate will automatically run update queries to save changes on Persistent object. See Java Persistence with Hibernate book for more details.
Hibernate will not run any SQL query to reflect any changes on this object. You can move this object into a persistent state by associating it with a hibernate session e.g. by calling save() or saveOrUpdate() method from a hibernate Session.
When an entity object moved to Persistence state it becomes the responsibility of Hibernate. Now if you make any change on entity object like change any attribute like the user.setName("Mike"), Hibernate will automatically run the update queries to persist the change into the database. A persistence object has a 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 a 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, the detached object was associated with Hibernate session in past and it has representation in the database, on the other hand, the Transient object is never associated with hibernate and it doesn't have any representation in the 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:
1. Database Representation
The main difference between transient, persistent, and detached objects comes from representation in the database. When an entity is first created, it goes to transient state and this time it doesn't have a representation in the database i.e. there will be no row corresponding to this object in Entity table. On the other hand, both Persistent and Detached objects have corresponding representation in the database.2. Association with Hibernate
Another key difference between transient, persistent, and detached objects comes from the fact that whether they are associated with the session or not. The transient object is not associated with the session, hibernate knows nothing about them. Similarly, the detached object is also not associated with the session, but a Persistent object is associated with the session.Hence any changes in the Persistent object will reflect in the database because Hibernate will automatically run update queries to save changes on Persistent object. See Java Persistence with Hibernate book for more details.
3. Impact of GC
Both transient and detached objects are eligible for garbage collection because they are not associated with the session, when GC will run they can be collected, but the persistent object is not eligible to garbage collection until the session is open because Hibernate Session object keeps a reference of Persistent object.4. State transition
When an entity is first created in an application using the new() operator, it remains in transient state. It can move to a 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 a 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 a hibernate entity objects life-cycle:
Here is a nice Hibernate state diagram which shows how state transition happens in Hibernate by calling different methods in a hibernate entity objects life-cycle:
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 the database.
That's all about the difference between a transient, persistent, and detached object in Hibernate. As a Hibernate developer, you must know how an object's life-cycle works in Hibernate i.e. when an object goes to the transient and detached state, how it goes to persistent state and what does Hibernate do when an object is in the persistent state i.e. automatically saving objects state into a database by running SQL queries.
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 (books)
- 5 Books to Learn Spring Framework (books)
- Why Hibernate Entity class should not be final in Java? (answer)
- 10 Hibernate Questions from Java Interviews (list)
- 5 Best Online Courses to learn Hibernate in depth (courses)
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 questions 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