Loading published curriculum…

14 · Hibernate and Spring Data JPA in depth / LESSON 63 OF 102

Entity states and the persistence context

An entity can be new, managed, detached, or removed; operations behave differently in each state.

Before this lesson: JPA versus Hibernate versus Spring Data

Understand the topic

The persistence context tracks managed instances and their identity. find of the same identifier within one context normally returns the same managed instance. After detach or context closure, changing that object no longer automatically updates the database.

persist makes a new entity managed. merge copies state into a managed instance and returns it; the supplied detached object does not become that returned managed instance. Avoid treating merge as permission to overwrite every field from an untrusted request.

EXAMPLE · JAVA

EntityManager fragment

// Inside an active transaction; Task is the entity from the persistence module.
Task managed = entityManager.find(Task.class, 1L);
entityManager.detach(managed);
Task copied = entityManager.merge(managed);
System.out.println(entityManager.contains(managed));
System.out.println(entityManager.contains(copied));

What to expect

Assuming task 1 exists, prints false then true. The merged result is the managed instance.

YOUR TURN

Put it into practice

  1. 01Observe contains before and after detach and merge.
  2. 02Change a detached value and compare behavior with a managed value inside a transaction.
  3. 03Verify: You can predict which changes are tracked and use the returned merge value correctly.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

An entity can be new, managed, detached, or removed; operations behave differently in each state.

  1. 01The persistence context tracks managed instances and their identity. find of the same identifier within one context normally returns the same managed instance. After detach or context closure, changing that object no longer automatically updates the database.
  2. 02persist makes a new entity managed. merge copies state into a managed instance and returns it; the supplied detached object does not become that returned managed instance. Avoid treating merge as permission to overwrite every field from an untrusted request.
  3. 03Trace the example: Assuming task 1 exists, prints false then true. The merged result is the managed instance.

A mistake worth avoiding

Changing a detached entity does not update the database.

DEBUGGING ROOM

Something broke. Where do you look?

Changing a detached entity does not update the database.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Does merge make the supplied detached object itself managed?

Choose one answer

BUILD IT YOURSELF

Observe contains before and after detach and merge.

  1. 1.Observe contains before and after detach and merge.
  2. 2.Change a detached value and compare behavior with a managed value inside a transaction.
  3. 3.Reproduce this failure, then repair it: Changing a detached entity does not update the database.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Does merge make the supplied detached object itself managed?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

You can predict which changes are tracked and use the returned merge value correctly.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references