Persistence Context
A Persistence Context keeps a managed view of objects
A persistence context is an area in which JPA tracks Entities. If an object is managed, the context knows its persistent identity and current state. That is why finding the same Entity again inside one context can return the object already known to it.
User first = entityManager.find(User.class, 42L);
User second = entityManager.find(User.class, 42L);
System.out.println(first == second);
Within one persistence context, one persistent identity is represented by one managed instance. This gives JPA a stable object view and lets it track changes.
Dirty checking turns object changes into updates
User user = entityManager.find(User.class, 42L);
user.setName("Mira");
A managed object usually does not need a separate update() call.
The provider sees the changed state and generates the required update when the context is synchronized with the database.
This change tracking is commonly called dirty checking.
flush is synchronization, not transaction completion
user.setName("Mira");
entityManager.flush();
flush() forces accumulated changes to be synchronized with the database,
but by itself it does not mean that the transaction completed successfully.
If the transaction is later rolled back, its changes must not be considered committed.