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 cascade operation propagates a selected entity lifecycle operation across a JPA association. Cascading is not enabled by default: associated entities otherwise have independent lifecycles, even when they reference each other. section 13.1

Persist an item and its bids

For an Item with a one-to-many collection of Bid entities, set cascade = CascadeType.PERSIST on the collection. Create the item and bids, link them, and save the item; the referenced bids become persistent without separate repository saves. In this bidirectional mapping, Bid.item supplies the foreign-key value. section 8.3.3

Listing 8.21 Cascading persistent state from Item to all bids
Path: Ch08/mapping-associations/src/main/java/com/manning/javapersistence
 /ch08/onetomany/cascadepersist/Item.java
 
@Entity
public class Item {
    // . . .
 
    @OneToMany(mappedBy = "item", cascade = CascadeType.PERSIST)
    private Set<Bid> bids = new HashSet<>();
    //  . . .
}

The mapping enables persist cascading from Item to its bids and identifies Bid.item as the other side of the association. section 8.3.3

Choose cascade options according to the operations that should propagate:

  • PERSIST makes associated entities persistent; REMOVE removes them when the parent is removed. These operations take effect at flush time. section 13.1.1
  • DETACH detaches associated entities from the persistence context; MERGE merges associated transient or detached entities. section 13.1.1
  • REFRESH propagates refresh; Hibernate also provides a REPLICATE cascade for Session.replicate(). section 13.1.1

Use removal cascading carefully. For example, adding CascadeType.REMOVE to Item.bids allows deleting an item to remove its bids. A many-to-one cascade in the opposite direction is often inappropriate: a bid normally refers to an item that already exists. section 8.3.3

Orphan removal is a separate mapping switch: with orphanRemoval = true, removing a bid from the collection can remove it from the database. Other in-memory references to that bid are not automatically cleaned up. Database-level ON DELETE CASCADE is another mechanism; Hibernate’s @OnDelete affects schema generation. section 8.3.3

A collection mapping also provides navigational access, but adds code and complexity. Cascade is optional; it should express a real lifecycle dependency rather than alone justify mapping the collection. section 8.3.2

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains operation-specific propagation, flush timing, mapping direction, orphan removal, and database-level deletion.
Examples
Maps Item-to-Bid persistence and removal, then removes a bid from a collection with orphan removal.
What you can do after
Operate and configure JPA cascade options for associated entities while distinguishing parent deletion from orphan removal.
How it connects
Connects entity lifecycle transitions with one-to-many associations, persistence contexts, orphan removal, and foreign keys.

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.