liveBook · Manning

On delete cascade

As explained in Java Persistence with Spring Data and Hibernate epub

ON DELETE CASCADE is a relational foreign-key action that automatically deletes referencing rows when the referenced row is deleted.

ON DELETE CASCADE makes the database delete dependent rows automatically when their referenced row is deleted. A foreign key such as BID.ITEM_ID can remove every matching BID when its ITEM is deleted, preserving referential integrity without loading dependent entities into application memory. section 8.3.3

Distinguish database cascading from application-managed association operations:

  • Database cascading can replace multiple dependent DELETE statements with one DELETE against the referenced table. section 8.3.3
  • Hibernate's CascadeType.REMOVE performs ORM-managed removal, whereas ON DELETE CASCADE affects database behavior and does not change Hibernate's runtime behavior. section 8.3.3
  • Hibernate can generate the foreign-key action with @OnDelete, but support must be checked for the selected DBMS and configuration. section 8.3.3

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Defines database-side deletion and schema annotation, but stops short of changing Hibernate runtime behavior.
Examples
Demonstrates Item and Bid deletion with H2, Hibernate @OnDelete, and a generated foreign-key cascade.
What you can do after
Operate/configure Hibernate schema generation with @OnDelete and verify whether the target DBMS supports the resulting foreign-key action.
How it connects
Connects foreign keys with referential integrity, Hibernate schema generation, and ORM cascade removal.

Also explained in

Compare how 2 books explain on delete cascade →

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.