EntityManager.find() method will look for the entity in persistence context before 2nd-level cache or database. However, CriteriaQuery or JPQL query will fetch data from database, and put entities in persistence context and 2nd-level cache.
CriteriaUpdate, CriteriaDelete, JPQL update/delete and native queries are not synchronized with persistence context. Affected entities should be refreshed or detached if necessary.
Synchronized Persistence Context HTTP request Begin transaction ------------------------------- Add a new entity Employee Commit Transaction HTTP request Begin transaction ------------------------------- Add a list of phone numbers to the employee Commit Transaction HTTP request Begin transaction ------------------------------- Upload a list of photos for the employee Commit Transaction
EntityManagerFactory emf = Persistence.createEntityManagerFactory(); EntityManager em = emf.createEntityManager(SynchronizationType.UNSYNCHRONIZED); // persist new entities, merge or remove entities ... // join to transaction em.joinTransaction(); EntityTransaction transaction = em.getTransaction(); transaction.commit();After transaction is completed(committed or rolled back), an unsynchronized persistence context will be detached from transaction, and it needs to be explicitly joined to transaction again if necessary. For example,
Unsynchronized Persistence Context HTTP request ------------------------------- Add a new entity Employee HTTP request ------------------------------- Add a list of phone numbers to the employee HTTP request ------------------------------- Upload a list of photos for the employee HTTP request Begin Transaction ------------------------------- Join to transaction, save the Employee entity and associated phone numbers and photos to database. Commit Transaction