liveBook · Manning

Removed state

As explained in Java Persistence with Spring Data and Hibernate epub

A removed state is an entity-instance state in which a persistence provider has scheduled the instance's database representation for deletion.

A removed state marks an entity instance whose database representation is scheduled for deletion by a persistence provider. The instance enters this state after EntityManager.remove() or after removing a mapped collection reference with orphan removal enabled. section 10.1.1

The removal lifecycle has several observable consequences:

  • The provider queues deletion until the unit of work completes; transaction commit synchronizes the state transition and executes SQL DELETE. section 10.2.5
  • The removed instance is no longer persistent, so EntityManager.contains(instance) returns false. section 10.2.5
  • References should normally be discarded after application work, such as rendering a removal-confirmation screen. section 10.1.1
  • Hibernate fully initializes a proxy during removal and runs lifecycle callbacks or entity listeners required for the transition. section 10.2.5

Canceling a queued deletion

Item item = em.find(Item.class, ITEM_ID);\nem.remove(item);\nassertFalse(em.contains(item));\nem.persist(item); // cancel before flush\nem.getTransaction().commit(); section 10.2.5

Figure 10.1 Entity instance states and their transitions

Entity instance states and their transitions provide a compact map of how removal relates to transient, persistent, and detached states. section 10.1.1

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains removal through queuing, containment checks, proxy initialization, callbacks, commit-time SQL, and optional identifier rollback.
Examples
Uses EntityManager.find(), remove(), contains(), persist(), proxy loading, orphan removal, and a SimpleTransitionsTest Item example.
What it assumes
Assumes basic entity identity and persistence-context concepts while distinguishing removed from transient, persistent, and detached states.
What you can do after
Operate/configure deletion with EntityManager, verify removed membership, cancel before flush, and understand commit-time SQL DELETE behavior.
How it connects
Connects entity lifecycle, persistence context, EntityManager, orphan removal, transactions, proxies, lifecycle callbacks, and transient state.

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.