The persistence context
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
explain the persistence context as an identity map and a unit of work.
Planned topics
- The persistence context is a per-transaction workspace holding the entities you loaded.
- Identity map: asking for the same id twice returns the same Java object, with no second query.
- Unit of work: it remembers the original state; at flush time it compares and writes only what changed.
- Flush means "send pending SQL now"; commit makes it permanent.
- Entities outside the workspace are detached: changes to them are not saved.
Draft outline details
Demonstration
Product a = em.find(Product.class, 1L); // SQL select
Product b = em.find(Product.class, 1L); // no SQL, from the identity map
System.out.println(a == b); // true
a.setName("New"); // tracked; update at flushCheck your understanding
Why does the second find issue no SQL?
Show answer
The entity is already in the persistence context.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.