liveBook · Manning

Transitive persistence

As explained in Java Persistence with Spring Data and Hibernate epub

Transitive persistence is the propagation of an entity-persistence operation to associated entities in the object graph.

Transitive persistence makes a persistence operation on one entity propagate to associated entities. Without it, new associated entities have independent lifecycles: saving an Item does not, by itself, save its new Bids. Each Bid must be saved separately. Cascading reduces those calls, but the entities still need to be linked correctly in memory. 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<>();
    //  . . .
}

Place CascadeType.PERSIST on Item.bids to make new Bids persistent when their Item is persisted. section 8.3.3

Save an Item and its Bids

Create the Item and Bids, set each Bid.item, and add each Bid to Item.bids. With CascadeType.PERSIST on the collection, one itemRepository.save(item) replaces separate saves for the Bids. At commit, Hibernate examines the managed Item’s collection; the BID.ITEM_ID foreign-key value comes from each Bid.item property. section 8.3.3

Choose cascades to match the relationship’s lifecycle:

  • CascadeType.REMOVE propagates deletion from an Item to its Bids. By contrast, orphanRemoval = true deletes a Bid removed from the Item’s collection. Neither operation cleans up references held elsewhere in memory automatically. section 8.3.3
  • A bidirectional one-to-one association can cascade persistence from a User to its shipping Address; the shown shared-primary-key mapping also uses a Hibernate-specific identifier generator. section 9.1.2
  • A Category can cascade persistence to new Items in a many-to-many association. Saving the Categories can then persist the linked network, but REMOVE, ALL and orphan deletion are not appropriate when related entities can be shared. section 9.3.1

Cascade deliberately: it can simplify code while having a serious performance consequence. Cascading from a Bid to its Item is usually a poor fit when the Item must already exist; do not treat every association as a shared lifecycle. section 8.3.3

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Works through mappings, repository calls and commit-time behavior, then distinguishes persistence from removal and orphan-removal behavior.
Examples
Shows Item–Bid saves, User–Address shared-primary-key persistence and Category–Item many-to-many saves.
What it assumes
Assumes familiarity with Java entities, association mappings and repository saves; explains why linked new entities otherwise require separate persistence.
What you can do after
Apply it via JPA cascade mappings to reduce explicit saves while choosing propagation direction and deletion behavior deliberately.
How it connects
Connects entity lifecycles, bidirectional associations, CascadeType.PERSIST, orphan removal and many-to-many mappings.

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.