Files
hibernate-demo/cascade-types
..

cascade-types

Companion module for the ankurm.com article JPA Cascade Types in Hibernate 7: A Decision Tree (Because the Wrong One Will Delete Your Data). Every claim in the article comes from a test here; each test writes what it observed to output/. Plain Hibernate on an in-memory H2: no Spring, no Docker. The same 14 tests also run on Hibernate 8.0.0.CR1.

Versions

Component Version Notes
Hibernate ORM 7.4.15.Final newest 7.x GA listed in maven-metadata.xml on 2026-11-15
Hibernate ORM (second run) 8.0.0.CR1 candidate release, not GA; needs Jakarta Persistence 4.0.0-M8
Jakarta Persistence 3.2.0 4.0.0-M8 for the Hibernate 8 run
H2 2.4.240 in memory
JUnit / AssertJ 6.0.3 / 3.27.7
Java 25 (Temurin 25.0.5+8)

Run

./scripts/run-all.sh      # both Hibernate versions; regenerates everything in output/
mvn clean test            # Hibernate 7.4.15.Final only
mvn clean test -Dhibernate.version=8.0.0.CR1 -Dpersistence.version=4.0.0-M8 -Dh.label=h8

Entities

Entities Mapping Used by
Invoice / InvoiceLine @OneToMany(cascade = ALL, orphanRemoval = true): composition T02, T03, T06, T07
Basket / BasketItem cascade = {PERSIST, REMOVE}, no orphanRemoval T03
Folder / Doc cascade = PERSIST + orphanRemoval = true, no REMOVE T03
AppUser, ProjectBad, ProjectBadDb, ProjectGood the incident: cascade = ALL on a shared @ManyToOne, with and without ON DELETE CASCADE, and the fix T04, T06, T08
Writer, Comment, CommentBad bad vs improved: cascade flows down, never up T04
Student, CourseBad, CourseGood @ManyToMany: ALL vs {PERSIST, MERGE} T05
Person, Profile, Address @OneToOne: owned profile cascades, shared address does not T05
Ticket / Agent @ManyToOne(cascade = {PERSIST, MERGE}) T05
Ledger, Vault (+ entries) @OnDelete(CASCADE) alone, and with cascade = ALL T06
Probe / Leaf one @OneToOne per cascade type, to find which operation travels along which T09
Board / Card mandatory (optional = false) foreign key T08

Output

Each test writes output/NN-name-h7.txt (Hibernate 7.4.15.Final) and, on the second run, output/NN-name-h8.txt (8.0.0.CR1). Transcript lines starting sql> are the SQL strings Hibernate prepared, recorded through a StatementInspector; raw JDBC used by the tests to count rows is not recorded.

File What it shows
01-api-shape JPA and Hibernate cascade enums, deprecations, where the cascade engine classes live
02-composition invoice + lines: persist, unlink one line, remove
03-orphan-vs-remove the two triggers: unlinking a child, removing the parent
04-incident, 04b-comment-author cascade = ALL on a shared @ManyToOne in three schemas, then the fix
05a-many-to-many, 05b-one-to-one, 05c-many-to-one the decision tree, one relationship type each
06a-cascade-remove-cost 1,000 lines: cascade REMOVE vs bulk delete first
06b-on-delete-cascade @OnDelete, loaded children, and a cascade that exists only in the schema
07a-replace-collection, 07b-merge-emptied-collection orphanRemoval failure modes
08-delete-order parent removed before child in one flush; Hibernate 8 flush queue
09-cascade-matrix persist / merge / remove / refresh / detach against each cascade type

There is deliberately no docs/ folder for this module; the explanations are in the article.