Card 07/ 07

RecapDifficulty: Advanced1 min

Recap: The JPA Entity Lifecycle

Four states, the operations between them, insert timing, detached changes, and refresh or clear.

The JPA entity lifecycle comes down to the five ideas below, each listed with the card that covers it.

What the topic established, and which card it was on
The ideaWhere
An entity is transient, managed, detached or removed, and only managed changes are writtenJPA Entity States: Transient, Managed, Detached and Removed
contains() shows the real state; merge() returns a different, managed objectTracing One Entity Through the JPA Lifecycle
With IDENTITY ids, persist() inserts at once; with SEQUENCE, at flushWhen persist() Inserts: IDENTITY vs SEQUENCE Ids
Changes to a detached entity are never writtenDetached Entities: Changes That Never Reach the Database
refresh() reloads one entity; clear() detaches all and reads nothingrefresh() vs clear() in JPA

When a JPA change goes missing, or appears when you did not expect it, the entity's state is the first thing to check.