ENABLE CHANGE TRACKING FOR ENTITIES

We know that Hibernate works with plain Java objects (POJO). In raw form (without hibernate specific annotations), hibernate will not be able khổng lồ identify these java classes. But when these POJOs are properly annotated with required annotations then hibernate will be able to identify them và then work with them e.g. store in the database, update them, etc. These POJOs are said to lớn mapped with hibernate.

Bạn đang xem: Enable change tracking for entities

1. Entity Lifecycle States

Given an instance of a class that is mapped to Hibernate, it can be in any one of four different persistence states (known as hibernate entity lifecyle states):

TransientPersistentDetachedRemoved1.1. Transient

Transient entities exist in heap memory as normal Java objects. Hibernate does not manage transient entities or persist changes done on them.

*
Transient objects are independent of Hibernate

To persist the changes lớn a transient entity, we would have lớn ask the hibernate session to lớn save the transient object khổng lồ the database, at which point Hibernate assigns the object an identifier và marks the object as being in persistent state.

1.2. Persistent

Persistent entities exist in the database, & Hibernate manages the persistence for persistent objects.

*
Persistent objects are maintained by Hibernate

If fields or properties change on a persistent object, Hibernate will keep the database representation up khổng lồ date when the application marks the changes as lớn be committed.

1.3. Detached

Detached entities have sầu a representation in the database, but changes khổng lồ the entity will not be reflected in the database, & vice-versa. This temporary separation of the entity and the database is shown in the image below.

*
Detached objects exist in the database but are not maintained by Hibernate

A detached entity can be created by closing the session that it was associated with, or by evicting it from the session with a Gọi lớn the session’s evict() method.

Xem thêm: Thảo Dược Hồng Hạnh Webtretho, Khách Hàng Đã Sử Dụng Thảo Dược Hồng Hạnh


One reason you might consider doing this would be lớn read an object out of the database, modify the properties of the object in memory, and then store the results some place other than your database. This would be an alternative to lớn doing a deep copy of the object.

In order lớn persist changes made to lớn a detached object, the application must re-attach it to a valid Hibernate session. A detached instance can be associated with a new Hibernate session when your application calls one of the load(), refresh(), merge(), update(), or save() methods on the new session with a reference lớn the detached object.

After the method Gọi, the detached entity would be a persistent entity managed by the new Hibernate session.

1.4. Removed

Removed entities are objects that are being managed by Hibernate (persistent entities, in other words) that have sầu been passed khổng lồ the session’s remove() method.

When the application marks the changes held in the session as to lớn be committed, the entries in the database that correspond khổng lồ removed entities are deleted.

Now let’s not note down the take-aways from this tutorial.

2. Conclusion

Newly created POJO object will be in the transient state. Transient entity doesn’t represent any row of the database i.e. not associated with any session object. It’s plain simple java object.Persistent entity represents one row of the database and always associated with some unique hibernate session. Changes to persistent objects are tracked by hibernate và are saved inkhổng lồ database when commit gọi happen.Detached entities are those who were once persistent in the past, và now they are no longer persistent. To persist changes done in detached objects, you must re-attach them lớn hibernate session.Removed entities are persistent objects that have been passed khổng lồ the session’s remove() method & soon will be deleted as soon as changes held in the session will be committed to database.

Happy Learning !!