liveBook · Manning

Cascading delete

As explained in Java Persistence with Spring Data and Hibernate epub

Cascading deletion is the propagation of an entity removal operation to associated entities.

Cascading deletion propagates removal from an owning entity to associated entities. For an Item with dependent Bid entities, deleting the Item can remove every associated Bid automatically. section 8.3.3

Choose the deletion mechanism according to lifecycle ownership and database responsibilities:

  • Configure JPA removal with cascade = {CascadeType.PERSIST, CascadeType.REMOVE}; deleting an Item through a repository or EntityManager removes its loaded Bid entities. section 8.3.3
  • Expect entity-level cascading to load the collection and issue individual deletes, which can be less efficient than one SQL DELETE. section 8.3.3
  • Use orphanRemoval = true when removing a Bid from the Item collection should delete that orphan, not merely when deleting the Item. section 8.3.3

Cascade Item removal

@OneToMany(mappedBy = "item", cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) private Set<Bid> bids = new HashSet<>(); itemRepository.delete(item);

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains JPA lifecycle cascading, orphan removal, SQL efficiency, and database constraints, but does not detail transaction internals.
Examples
Uses Item–Bid mappings, repository deletion, orphan removal, and Hibernate @OnDelete configuration.
What it assumes
Assumes basic entity associations, repositories, and persistence operations such as remove and delete.
What you can do after
Configure and evaluate entity-level or database-level deletion propagation for associated entities.
How it connects
Connects JPA entity associations with Hibernate, Spring Data JPA, orphan removal, foreign keys, and SQL DELETE statements.

Also explained in

Compare how 3 books explain cascading delete →

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.