liveBook · Manning

Cascade operation

As explained in Java Persistence with Spring Data and Hibernate epub

Cascade is a JPA association-mapping configuration that propagates selected entity lifecycle operations or state changes to associated entities.

A JPA association does not make the lifecycles of its entities dependent. A new Item and its new Bids normally require separate save operations, even when each Bid refers to the Item. The association’s cascade option selects which state transitions should propagate from one entity to its associated entities. section 8.3.3 section 13.1

Persist an item and its bids

Map the collection with `@OneToMany(mappedBy = "item", cascade = CascadeType.PERSIST)`. Add each new Bid to the Item’s collection, then call `itemRepository.save(item)` rather than saving every Bid separately. At commit time, Spring Data JPA using Hibernate examines the managed Item and makes its referenced Bids persistent. The `BID.ITEM_ID` foreign-key value comes from each Bid’s `item` property; `mappedBy` identifies that side of the association. section 8.3.3

Choose the direction to match the dependency. Cascading from an Item to its Bids can be useful; cascading a Bid’s save to its Item is usually inappropriate when the Item must already exist. A bidirectional collection also provides navigational access, but adds mapping and Java-code complexity. If a dependent object does not need a shared entity identity, consider an embeddable value type instead. section 8.3.2 section 8.3.3

Select operations deliberately rather than treating every association change alike:

  • `PERSIST` propagates persistence; `REMOVE` propagates deletion. With both on the Item collection, deleting an Item can remove its Bids without deleting them individually. section 8.3.3
  • `orphanRemoval = true` addresses a different action: removing a Bid from the Item’s collection. A demonstrated save leaves one fewer Bid in the database, but another already-loaded collection can still hold its old in-memory reference. section 8.3.3
  • `DETACH`, `MERGE`, and `REFRESH` propagate their corresponding EntityManager operations. Hibernate also identifies `REPLICATE` with `Session#replicate()`. section 13.1.1
Table 13.1 Cascading options for entity association mappings (view table figure)

Option

Description

CascadeType.PERSIST

When an entity instance is stored with EntityManager #persist(), at flush time any associated entity instances are also made persistent.

CascadeType.REMOVE

When an entity instance is deleted with EntityManager #remove(), at flush time any associated entity instances are also removed.

CascadeType.DETACH

When an entity instance is evicted from the persistence context with EntityManager#detach(), any associated entity instances are also detached.

CascadeType.MERGE

When a transient or detached entity instance is merged into a persistence context with EntityManager#merge(), any associated transient or detached entity instances are also merged.

CascadeType.REFRESH

When a persistent entity instance is refreshed with EntityManager#refresh(), any associated persistent entity instances are also refreshed.

CascadeType.REPLICATE

When a detached entity instance is copied into a database with Session#replicate(), any associated detached entity instances are also copied.

CascadeType.ALL

This is shorthand to enable all cascading options for the mapped association.

Compare the cascade options with the EntityManager or Session operations whose state transitions they propagate. section 13.1.1

Do not confuse entity-operation cascades with database-level deletion. Hibernate’s `@OnDelete` can generate a foreign key with `ON DELETE CASCADE`; its cited use affects schema generation. Use transitive deletion carefully, especially when deciding whether associated entities should survive their owner. section 8.3.3

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Works through association mappings, repository calls, commit-time persistence, removal, orphan removal, and an option-to-operation comparison.
Examples
Uses Item and Bid mappings, Spring Data JPA repository calls, and a collection-removal case with a stale in-memory reference.
What it assumes
Assumes familiarity with entities, associations, and repository saves while explaining that associated entities retain independent lifecycles by default.
What you can do after
Operate/configure it by selecting association cascade options for persistence or deletion and distinguishing collection orphan removal from owner deletion.
How it connects
Connects one-to-many and many-to-one associations with entity lifecycles, orphan removal, embeddable value types, and foreign-key deletion.

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.