In this post, I will try to explain the context and entity terms that are used in the ORM technique in Hibernate. Also I will try to explain the basics of managing an entity within Hibernate.

In software development** persistence **is the term that refers to the information that keeps existing even after the application is not running anymore. Persistence context in Hibernate is the information related to the objects to be managed between the application and the database.

Hibernate manages the lifecycle of objects in the persistence context by being aware of their states and their relations to the persistence store. The persistence context act as a staging layer between the application and the database, once the transaction is committed in the application the changes are then applied to the database.

The managed objects in the context are called Entities. There are 4 states of entities in the persistence context.

  • new or **transient: **This is the initial state of the object and there are no representation for this object in the database yet.
  • managed or **persistent: **In this state, the entity is associated with the context and the entity represents a database table row.
  • **detached: **In this state, the entity still has a representation in the database but it is not managed by any context. This usually happens when we either close the session or clear its cache. The changes made to the data won’t affect the database since the entity is not managed by any context.
  • **removed: **In this state,theentity is marked for removal from the database by the managing context.

#hibernate-context #persistence-context #entity #hibernate-entity #hibernate-entitymanager

Hibernate Tutorial — 2. Context & Entity
1.30 GEEK