DEV Community

Cover image for Hibernate - 4 Entity States
Yiğit Erkal
Yiğit Erkal

Posted on

Hibernate - 4 Entity States

What is entity? 💬❓

Entity are our domain classes corresponding to the tables in the database. For a java class to be an entity, at a minimum, a field with the @Entity annotation, the package access level no-args constructor, and the @id annotation is required.

Here is how an entity class looks...

@Entity
class ExampleObject {
  @Id
  @GeneratedValue
  private Long id;
  private String value;
}
Enter fullscreen mode Exit fullscreen mode

How things work in the background? ⚙️

Persistence Context is the structure that manages the entity objects and the entity lifecycle. It is between the database and the application. Persistence Context holds references to entity objects. EntityManager in JPA and Session object in Hibernate is located inside the as Persistence Context as an internal structure. The next access to the entities in the Persistence Context does not go to the database, but the same entity reference is returned to the application. Also known as first level cache.
Persistence Context monitors the loaded entity and marks it as dirty (modified) when there is a change in the entity object (please check my other post about dirty check), and automatically reflects the changes to the database at certain stages. In order to detect changes made on the Entity object, a snapshot copy of each object uploaded to the Persistence Context is also kept. In order to follow the changes on the entities, the session in which the entities are loaded must be open and the relations of the loaded entities with this session must continue. When the session is closed, the snapshot copy of the relevant entity is also lost.

Why don't you tell us the Entity Lifecycle?? 😡

Every entity object has a lifecycle and a state in which it exists. These are Transient, managed, detached or removed. It is important to understand these situations in order to use Hibernate and other JPA implementations properly.

https://vladmihalcea.com/wp-content/uploads/2014/07/hibernateentitystates1.png

New Transient
When an entity object is created and has no primary key, its state is new transient. Objects in the new transient state do not have any record counterparts in the database and these objects are not related to the Persistence Context.

Car car = new Car();
car.setName(“Ferrari”);
Enter fullscreen mode Exit fullscreen mode

Managed
Entities that have a database counterpart, exist in the active session and are managed by the Persistence Context are in this state. The state changes of the entities in the managed state are tracked as long as the Persistence Context is turned on, and these changes are automatically reflected in the database at a certain stage. During this state Hibernate manages the object and saves your changes, if you commit them.

Session session = HibernateSessionFactory.currentSession();
transaction = session.beginTransaction();
session.save(car);
car.setName(“Fiat”);
transaction.commit();
Enter fullscreen mode Exit fullscreen mode

Detach
The state of entities with database counterparts but not in Persistence Context is detach. When the Persistence Context is turned off, the state of the entities in the managed state becomes detach state. Hibernate does not track the changes made on the entities in the Detach state.

Removed
When the entities in the managed or detached state are deleted, they move to the removed state.


Please check the following link for further details in order move from any state to another state to observe how an entity state changes...

Top comments (0)