The persistence context internally has a cache storage which is in the form of a Map data structure.
@Id.An Action Queue in Hibernate temporarily stores database operations (like inserts, updates, and deletes) using deferred execution, meaning the operations are queued and only executed in the correct order when the transaction is committed.
Dirty checking in Hibernate is a mechanism that automatically detects changes made to managed entities during a transaction. Before committing, Hibernate compares the current state of the entities with their LoadedState (an original state in the persistence context), and if any changes are detected, it automatically generates and executes the necessary SQL UPDATE statements to synchronize the database with the changes.
1. Save entity

@Test
@DisplayName("Save Entity")
void test1() {
EntityTransaction et = em.getTransaction();
et.begin();
try {
Memo memo = new Memo();
memo.setId(1L);
memo.setUsername("Robbie");
memo.setContents("1차 캐시 Entity 저장");
em.persist(memo); // saves memo in the cache
et.commit(); // saves to data base
} catch (Exception ex) {
ex.printStackTrace();
et.rollback();
} finally {
em.close();
}
emf.close();
}
2. Inquire entity
When the entity inquired is NOT in the cache:
The even manager will search for the data in the database through a select query and save it in the cache and then finally return the entity.
When the entity inquired IS in the cache:
It will check if Entity is the expected type to be return and simply return the object without additional searches in the database

@Test
@DisplayName("Entity inquiry : Id does not exist in cache")
void test2() {
try {
Memo memo = em.find(Memo.class, 1); // inquire entity
System.out.println("memo.getId() = " + memo.getId());
System.out.println("memo.getUsername() = " + memo.getUsername());
System.out.println("memo.getContents() = " + memo.getContents());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
em.close();
}
emf.close();
}
3. Delete entity
For delete a similar process happens as the inquiry. If the entity to delete is in the cache, they are simply deleted but if not they are called from database, saved in the cache, then deleted.
em.remove(memo);
💡 Actual changes to the SQL are done after
et.commit();

1. Transient
It has just been instantiated and is not associated with any persistence context, meaning it is not yet being tracked by the EntityManager.
2. Managed
When it is associated with a persistence context, and changes to it are tracked and automatically synchronized with the database.
3. Detached
When it was previously managed but is no longer associated with a persistence context, meaning it is no longer tracked or synchronized with the database.
4. Removed
When it has been marked for deletion within the persistence context, and it will be removed from the database upon the next flush or commit.