Skip to content
Ameen Abozaid
Planned lessonJNR-027-L03

The persistence context

  • Lesson 3 of 4
  • Junior
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

java
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 flush

Check 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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.