liveBook · Manning

Entity state

As explained in Java Persistence with Spring Data and Hibernate epub

The lifecycle state in which an entity instance is associated with a persistence context and tracked for persistence synchronization.

An entity’s state describes its relationship to a persistence context during a unit of work. A persistent instance is managed by that context and has a database representation—or will have one when the unit of work completes. The context tracks changes for persistence synchronization. section 10.1 section 10.1.1

The lifecycle has four states:

  • Transient: created with `new` and not yet made persistent. `EntityManager.persist()` or an enabled cascade can move it into persistent state. section 10.1.1
  • Persistent: associated with a persistence context. A query, identifier lookup, or navigation from another managed instance can also produce a persistent instance. section 10.1.1
  • Removed: marked for deletion through `EntityManager.remove()` or orphan removal on a mapped collection. section 10.1.1
  • Detached: no longer managed, for example after `EntityManager.detach()` or context closure. Changes made while detached do not update the database unless merged into a new context. section 10.3.3

Making an Item persistent

Within a unit of work: `Item item = new Item(); item.setName("Some Item"); em.persist(item);` The call associates `item` with the current persistence context. Hibernate normally issues the `INSERT` when the transaction commits and the context flushes, though an identifier generator may require an immediate insert. section 10.2.2

Use `em.contains(entity)` to check whether an instance is managed by that context. An identifier can help distinguish transient from detached instances, but identifier assignment may wait until flush; primitive identifier properties also require care. Avoid basing `equals()` and `hashCode()` solely on a generated identifier: its change on persistence can break a `Set`. section 10.2.2 section 10.3.2

Figure 10.1 Entity instance states and their transitions

The state-transition diagram provides a quick map of the four lifecycle states and their transitions. section 10.1.1

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Defines four lifecycle states, demonstrates persistence and detachment operations, and explains insert timing and state-detection caveats.
Examples
Uses a newly created Item made persistent and a User detached after an identifier lookup.
What it assumes
Assumes Java object creation and database identifiers while introducing persistence lifecycle states and the persistence context.
What you can do after
Diagnose/debug with it by checking context membership and identifier availability before treating an instance as managed or detached.
How it connects
Connects entity state to persistence contexts, cascading, identifier generation, orphan removal, and entity equality.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.