Add cascade-types module: JPA cascade types and orphanRemoval on Hibernate 7.4.15.Final and 8.0.0.CR1 (incident reproduced in three schemas, cascade matrix, @OnDelete, delete order)

Plain Hibernate on H2, 14 tests, transcripts in cascade-types/output (h7 and h8). No docs folder.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b5sy5qAYYdeYeS1xLW
This commit is contained in:
2026-11-15 01:22:39 +05:30
co-authored by Claude Sonnet 5.5
parent 0245c00033
commit bdae169002
72 changed files with 3247 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
# Cascade API shape in the shipped jars
# Hibernate ORM 7.4.15.Final, H2, JDK 25
== jakarta.persistence.CascadeType constants
ALL, PERSIST, MERGE, REMOVE, REFRESH, DETACH
== org.hibernate.annotations.CascadeType (Hibernate's own enum)
enum class: present, deprecated(since=7, forRemoval=true)
constants: [ALL, PERSIST, MERGE, REMOVE, REFRESH, DETACH, LOCK, REPLICATE, DELETE_ORPHAN]
has LOCK: true
@Cascade annotation: present, deprecated(since=7, forRemoval=true)
== Internal classes that do the cascading (where each lives in this jar)
Cascade org.hibernate.engine.internal/spi: present org.hibernate.cascade.internal/spi: absent
CascadingAction org.hibernate.engine.internal/spi: present org.hibernate.cascade.internal/spi: absent
CascadingActions org.hibernate.engine.internal/spi: present org.hibernate.cascade.internal/spi: absent
CascadeStyle org.hibernate.engine.internal/spi: present org.hibernate.cascade.internal/spi: absent
CascadeStyles org.hibernate.engine.internal/spi: present org.hibernate.cascade.internal/spi: absent
CascadePoint org.hibernate.engine.internal/spi: present org.hibernate.cascade.internal/spi: absent
CascadeEntityPersister (a name the old article used): absent
+18
View File
@@ -0,0 +1,18 @@
# Cascade API shape in the shipped jars
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
== jakarta.persistence.CascadeType constants
ALL, PERSIST, MERGE, REMOVE, REFRESH, DETACH
== org.hibernate.annotations.CascadeType (Hibernate's own enum)
enum class: absent
@Cascade annotation: absent
== Internal classes that do the cascading (where each lives in this jar)
Cascade org.hibernate.engine.internal/spi: absent org.hibernate.cascade.internal/spi: present
CascadingAction org.hibernate.engine.internal/spi: absent org.hibernate.cascade.internal/spi: present
CascadingActions org.hibernate.engine.internal/spi: absent org.hibernate.cascade.internal/spi: present
CascadeStyle org.hibernate.engine.internal/spi: absent org.hibernate.cascade.internal/spi: present
CascadeStyles org.hibernate.engine.internal/spi: absent org.hibernate.cascade.internal/spi: present
CascadePoint org.hibernate.engine.internal/spi: absent org.hibernate.cascade.internal/spi: present
CascadeEntityPersister (a name the old article used): absent
@@ -0,0 +1,24 @@
# Composition: Invoice -> InvoiceLine, cascade = ALL + orphanRemoval = true
# Hibernate ORM 7.4.15.Final, H2, JDK 25
== persist(invoice) with two new lines, lines never persisted explicitly
-- persist(inv); flush()
sql> insert into Invoice (number,id) values (?,default)
sql> insert into InvoiceLine (description,invoice_id,id) values (?,?,default)
sql> insert into InvoiceLine (description,invoice_id,id) values (?,?,default)
raw rows: invoice=1 InvoiceLine=2
== remove one line from the collection (the invoice stays)
-- lines.remove(0); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from InvoiceLine where id=?
raw rows: invoice=1 InvoiceLine=1
== remove the invoice
-- remove(find(Invoice)); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from InvoiceLine where id=?
sql> delete from Invoice where id=?
raw rows: invoice=0 InvoiceLine=0
@@ -0,0 +1,24 @@
# Composition: Invoice -> InvoiceLine, cascade = ALL + orphanRemoval = true
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
== persist(invoice) with two new lines, lines never persisted explicitly
-- persist(inv); flush()
sql> insert into Invoice (number,id) values (?,default)
sql> insert into InvoiceLine (description,invoice_id,id) values (?,?,default)
sql> insert into InvoiceLine (description,invoice_id,id) values (?,?,default)
raw rows: invoice=1 InvoiceLine=2
== remove one line from the collection (the invoice stays)
-- lines.remove(0); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from InvoiceLine where id=?
raw rows: invoice=1 InvoiceLine=1
== remove the invoice
-- remove(find(Invoice)); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from InvoiceLine where id=?
sql> delete from Invoice where id=?
raw rows: invoice=0 InvoiceLine=0
@@ -0,0 +1,30 @@
# orphanRemoval vs cascade = REMOVE
# Hibernate ORM 7.4.15.Final, H2, JDK 25
== Trigger 1: child removed from the parent's collection, parent kept
-- Basket (cascade = {PERSIST, REMOVE}, no orphanRemoval): items.remove(0); flush()
sql> select b1_0.id from Basket b1_0 where b1_0.id=?
sql> select i1_0.basket_id,i1_0.id,i1_0.name from BasketItem i1_0 where i1_0.basket_id=?
raw rows: BasketItem=2 (still 2: nothing was deleted)
-- Invoice (cascade = ALL, orphanRemoval = true): lines.remove(0); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from InvoiceLine where id=?
raw rows: InvoiceLine=1 (now 1: the orphan was deleted)
== Trigger 2: the parent itself is removed
Folder maps cascade = PERSIST and orphanRemoval = true, and has no REMOVE at all
-- remove(folder); flush()
sql> select f1_0.id from Folder f1_0 where f1_0.id=?
sql> select d1_0.folder_id,d1_0.id,d1_0.title from Doc d1_0 where d1_0.folder_id=?
sql> delete from Doc where id=?
sql> delete from Doc where id=?
sql> delete from Folder where id=?
raw rows: doc=0 (0: orphanRemoval alone also deletes children when the parent goes)
-- Basket (REMOVE, no orphanRemoval): remove(basket); flush()
sql> select b1_0.id from Basket b1_0 where b1_0.id=?
sql> select i1_0.basket_id,i1_0.id,i1_0.name from BasketItem i1_0 where i1_0.basket_id=?
sql> delete from BasketItem where id=?
sql> delete from BasketItem where id=?
sql> delete from Basket where id=?
raw rows: BasketItem=0 basket=0
@@ -0,0 +1,28 @@
# orphanRemoval vs cascade = REMOVE
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
== Trigger 1: child removed from the parent's collection, parent kept
-- Basket (cascade = {PERSIST, REMOVE}, no orphanRemoval): items.remove(0); flush()
sql> select b1_0.id from Basket b1_0 where b1_0.id=?
sql> select i1_0.basket_id,i1_0.id,i1_0.name from BasketItem i1_0 where i1_0.basket_id=?
raw rows: BasketItem=2 (still 2: nothing was deleted)
-- Invoice (cascade = ALL, orphanRemoval = true): lines.remove(0); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from InvoiceLine where id=?
raw rows: InvoiceLine=1 (now 1: the orphan was deleted)
== Trigger 2: the parent itself is removed
Folder maps cascade = PERSIST and orphanRemoval = true, and has no REMOVE at all
-- remove(folder); flush()
sql> select f1_0.id from Folder f1_0 where f1_0.id=?
sql> select d1_0.folder_id,d1_0.id,d1_0.title from Doc d1_0 where d1_0.folder_id=?
sql> delete from Doc where id=?
sql> delete from Folder where id=?
raw rows: doc=0 (0: orphanRemoval alone also deletes children when the parent goes)
-- Basket (REMOVE, no orphanRemoval): remove(basket); flush()
sql> select b1_0.id from Basket b1_0 where b1_0.id=?
sql> select i1_0.basket_id,i1_0.id,i1_0.name from BasketItem i1_0 where i1_0.basket_id=?
sql> delete from BasketItem where id=?
sql> delete from Basket where id=?
raw rows: BasketItem=0 basket=0
+37
View File
@@ -0,0 +1,37 @@
# The incident: cascade = ALL on a @ManyToOne to a shared parent
# Hibernate ORM 7.4.15.Final, H2, JDK 25
== Foreign keys on the project tables (ON DELETE rule as H2 reports it)
ProjectBad : [FKC2MGHR59KQYACY2C4OQFD1XNP | NO ACTION]
ProjectBadDb : [FKDTYHCRAYDD8XEEGIE4S62FQ3P | CASCADE]
ProjectGood : [FKNLG47HDNK41TPFKBDSEGG8K9G | NO ACTION]
== 1. Testing: every project has its own user
-- remove(project 1); flush()
sql> select pb1_0.id,pb1_0.owner_id,pb1_0.title from ProjectBad pb1_0 where pb1_0.id=?
sql> delete from ProjectBad where id=?
sql> delete from AppUser where id=?
raw rows: AppUser=0 ProjectBad=0 (the user is gone, nothing failed)
== 2. Production, plain schema: the user has two projects
-- remove(first project); flush()
sql> select pb1_0.id,pb1_0.owner_id,pb1_0.title from ProjectBad pb1_0 where pb1_0.id=?
sql> delete from ProjectBad where id=?
sql> delete from AppUser where id=?
threw: org.hibernate.exception.ConstraintViolationException <- org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException
cause: Referential integrity constraint violation: "FKC2MGHR59KQYACY2C4OQFD1XNP: PUBLIC.PROJECTBAD FOREIGN KEY(OWNER_ID) REFERENCES PUBLIC.APPUSER(ID) (CAST(2 AS BIGINT))"; SQL statement:
raw rows after rollback: AppUser=1 ProjectBad=2
== 3. Production, schema with ON DELETE CASCADE on the foreign key
before: ProjectBadDb=2
-- remove(first project); flush()
sql> select pbd1_0.id,pbd1_0.owner_id,pbd1_0.title from ProjectBadDb pbd1_0 where pbd1_0.id=?
sql> delete from ProjectBadDb where id=?
sql> delete from AppUser where id=?
raw rows: ProjectBadDb=0 (both projects gone, no error)
== 4. The fix: ProjectGood, no cascade
-- remove(first project); flush()
sql> select pg1_0.id,pg1_0.owner_id,pg1_0.title from ProjectGood pg1_0 where pg1_0.id=?
sql> delete from ProjectGood where id=?
raw rows: ProjectGood=1 AppUser=1 new user(s) kept
+37
View File
@@ -0,0 +1,37 @@
# The incident: cascade = ALL on a @ManyToOne to a shared parent
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
== Foreign keys on the project tables (ON DELETE rule as H2 reports it)
ProjectBad : [FKC2MGHR59KQYACY2C4OQFD1XNP | NO ACTION]
ProjectBadDb : [FKDTYHCRAYDD8XEEGIE4S62FQ3P | CASCADE]
ProjectGood : [FKNLG47HDNK41TPFKBDSEGG8K9G | NO ACTION]
== 1. Testing: every project has its own user
-- remove(project 1); flush()
sql> select pb1_0.id,pb1_0.owner_id,pb1_0.title from ProjectBad pb1_0 where pb1_0.id=?
sql> delete from ProjectBad where id=?
sql> delete from AppUser where id=?
raw rows: AppUser=0 ProjectBad=0 (the user is gone, nothing failed)
== 2. Production, plain schema: the user has two projects
-- remove(first project); flush()
sql> select pb1_0.id,pb1_0.owner_id,pb1_0.title from ProjectBad pb1_0 where pb1_0.id=?
sql> delete from ProjectBad where id=?
sql> delete from AppUser where id=?
threw: org.hibernate.exception.ConstraintViolationException <- org.h2.jdbc.JdbcBatchUpdateException
cause: Referential integrity constraint violation: "FKC2MGHR59KQYACY2C4OQFD1XNP: PUBLIC.PROJECTBAD FOREIGN KEY(OWNER_ID) REFERENCES PUBLIC.APPUSER(ID) (CAST(2 AS BIGINT))"; SQL statement:
raw rows after rollback: AppUser=1 ProjectBad=2
== 3. Production, schema with ON DELETE CASCADE on the foreign key
before: ProjectBadDb=2
-- remove(first project); flush()
sql> select pbd1_0.id,pbd1_0.owner_id,pbd1_0.title from ProjectBadDb pbd1_0 where pbd1_0.id=?
sql> delete from ProjectBadDb where id=?
sql> delete from AppUser where id=?
raw rows: ProjectBadDb=0 (both projects gone, no error)
== 4. The fix: ProjectGood, no cascade
-- remove(first project); flush()
sql> select pg1_0.id,pg1_0.owner_id,pg1_0.title from ProjectGood pg1_0 where pg1_0.id=?
sql> delete from ProjectGood where id=?
raw rows: ProjectGood=1 AppUser=1 new user(s) kept
@@ -0,0 +1,25 @@
# Bad -> improved: CommentBad (cascade ALL to the author) vs Comment + Writer.comments
# Hibernate ORM 7.4.15.Final, H2, JDK 25
== Bad: a writer with two comments, delete one
-- remove(CommentBad first); flush()
sql> select cb1_0.id,cb1_0.author_id,cb1_0.text from CommentBad cb1_0 where cb1_0.id=?
sql> select w1_0.id,w1_0.name from Writer w1_0 where w1_0.id=?
sql> select c1_0.author_id,c1_0.id,c1_0.text from Comment c1_0 where c1_0.author_id=?
sql> delete from CommentBad where id=?
sql> delete from Writer where id=?
threw: org.hibernate.exception.ConstraintViolationException <- org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException
cause: Referential integrity constraint violation: "FKI5Y1RDJMEQ4UP89E3R112XDIT: PUBLIC.COMMENTBAD FOREIGN KEY(AUTHOR_ID) REFERENCES PUBLIC.WRITER(ID) (CAST(1 AS BIGINT))"; SQL statement:
raw rows after rollback: Writer=1 CommentBad=2
== Improved: cascade flows down from Writer to Comment, never up
-- remove(Comment one); flush()
sql> select c1_0.id,c1_0.author_id,c1_0.text from Comment c1_0 where c1_0.id=?
sql> delete from Comment where id=?
raw rows: Writer(bob)=1 Comment=1
-- remove(Writer bob); flush() (cascade ALL downward)
sql> select w1_0.id,w1_0.name from Writer w1_0 where w1_0.id=?
sql> select c1_0.author_id,c1_0.id,c1_0.text from Comment c1_0 where c1_0.author_id=?
sql> delete from Comment where id=?
sql> delete from Writer where id=?
raw rows: Comment=0
@@ -0,0 +1,25 @@
# Bad -> improved: CommentBad (cascade ALL to the author) vs Comment + Writer.comments
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
== Bad: a writer with two comments, delete one
-- remove(CommentBad first); flush()
sql> select cb1_0.id,cb1_0.author_id,cb1_0.text from CommentBad cb1_0 where cb1_0.id=?
sql> select w1_0.id,w1_0.name from Writer w1_0 where w1_0.id=?
sql> select c1_0.author_id,c1_0.id,c1_0.text from Comment c1_0 where c1_0.author_id=?
sql> delete from CommentBad where id=?
sql> delete from Writer where id=?
threw: org.hibernate.exception.ConstraintViolationException <- org.h2.jdbc.JdbcBatchUpdateException
cause: Referential integrity constraint violation: "FKI5Y1RDJMEQ4UP89E3R112XDIT: PUBLIC.COMMENTBAD FOREIGN KEY(AUTHOR_ID) REFERENCES PUBLIC.WRITER(ID) (CAST(1 AS BIGINT))"; SQL statement:
raw rows after rollback: Writer=1 CommentBad=2
== Improved: cascade flows down from Writer to Comment, never up
-- remove(Comment one); flush()
sql> select c1_0.id,c1_0.author_id,c1_0.text from Comment c1_0 where c1_0.id=?
sql> delete from Comment where id=?
raw rows: Writer(bob)=1 Comment=1
-- remove(Writer bob); flush() (cascade ALL downward)
sql> select w1_0.id,w1_0.name from Writer w1_0 where w1_0.id=?
sql> select c1_0.author_id,c1_0.id,c1_0.text from Comment c1_0 where c1_0.author_id=?
sql> delete from Comment where id=?
sql> delete from Writer where id=?
raw rows: Comment=0
@@ -0,0 +1,28 @@
# @ManyToMany: cascade ALL vs {PERSIST, MERGE}
# Hibernate ORM 7.4.15.Final, H2, JDK 25
== CourseBad (cascade = ALL): one student enrolled in two courses
-- remove(algebra); flush()
sql> select cb1_0.id,cb1_0.title from CourseBad cb1_0 where cb1_0.id=?
sql> select s1_0.course_id,s1_1.id,s1_1.name from course_bad_students s1_0 join Student s1_1 on s1_1.id=s1_0.student_id where s1_0.course_id=?
sql> delete from course_bad_students where course_id=?
sql> delete from Student where id=?
threw: org.hibernate.exception.ConstraintViolationException <- org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException
cause: Referential integrity constraint violation: "FKHFU87UUUVAK0UGR0D4KGGPNTC: PUBLIC.COURSE_BAD_STUDENTS FOREIGN KEY(STUDENT_ID) REFERENCES PUBLIC.STUDENT(ID) (CAST(1 AS BIGINT))"; SQL statement:
raw rows after rollback: CourseBad=2 Student=1
== CourseBad: the same, but the student is only in that one course
-- remove(chemistry); flush()
sql> select cb1_0.id,cb1_0.title from CourseBad cb1_0 where cb1_0.id=?
sql> select s1_0.course_id,s1_1.id,s1_1.name from course_bad_students s1_0 join Student s1_1 on s1_1.id=s1_0.student_id where s1_0.course_id=?
sql> delete from course_bad_students where course_id=?
sql> delete from Student where id=?
sql> delete from CourseBad where id=?
raw rows: Student 2 -> 1 (the student was deleted with the course)
== CourseGood (cascade = {PERSIST, MERGE})
-- remove(dance); flush()
sql> select cg1_0.id,cg1_0.title from CourseGood cg1_0 where cg1_0.id=?
sql> delete from course_good_students where course_id=?
sql> delete from CourseGood where id=?
raw rows: Student 3 -> 3 course_good_students=0
@@ -0,0 +1,28 @@
# @ManyToMany: cascade ALL vs {PERSIST, MERGE}
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
== CourseBad (cascade = ALL): one student enrolled in two courses
-- remove(algebra); flush()
sql> select cb1_0.id,cb1_0.title from CourseBad cb1_0 where cb1_0.id=?
sql> select s1_0.course_id,s1_1.id,s1_1.name from course_bad_students s1_0 join Student s1_1 on s1_1.id=s1_0.student_id where s1_0.course_id=?
sql> delete from course_bad_students where course_id=?
sql> delete from Student where id=?
threw: org.hibernate.exception.ConstraintViolationException <- org.h2.jdbc.JdbcBatchUpdateException
cause: Referential integrity constraint violation: "FKHFU87UUUVAK0UGR0D4KGGPNTC: PUBLIC.COURSE_BAD_STUDENTS FOREIGN KEY(STUDENT_ID) REFERENCES PUBLIC.STUDENT(ID) (CAST(1 AS BIGINT))"; SQL statement:
raw rows after rollback: CourseBad=2 Student=1
== CourseBad: the same, but the student is only in that one course
-- remove(chemistry); flush()
sql> select cb1_0.id,cb1_0.title from CourseBad cb1_0 where cb1_0.id=?
sql> select s1_0.course_id,s1_1.id,s1_1.name from course_bad_students s1_0 join Student s1_1 on s1_1.id=s1_0.student_id where s1_0.course_id=?
sql> delete from course_bad_students where course_id=?
sql> delete from Student where id=?
sql> delete from CourseBad where id=?
raw rows: Student 2 -> 1 (the student was deleted with the course)
== CourseGood (cascade = {PERSIST, MERGE})
-- remove(dance); flush()
sql> select cg1_0.id,cg1_0.title from CourseGood cg1_0 where cg1_0.id=?
sql> delete from course_good_students where course_id=?
sql> delete from CourseGood where id=?
raw rows: Student 3 -> 3 course_good_students=0
@@ -0,0 +1,23 @@
# @OneToOne: an owned profile cascades, a shared address does not
# Hibernate ORM 7.4.15.Final, H2, JDK 25
== Persisting a person with a new profile and a new, unsaved address
-- persist(person); flush()
sql> insert into Profile (bio,id) values (?,default)
sql> insert into Person (billingAddress_id,name,profile_id,id) values (?,?,?,default)
threw: java.lang.IllegalStateException <- org.hibernate.TransientPropertyValueException
cause: Persistent instance of 'com.ankurm.cascade.Person' references an unsaved transient instance of 'com.ankurm.cascade.Address' (persist the transient instance before flushing) [com.ankurm.cascade.Person.billingAddress -> com.ankurm.cascade.Address]
raw rows after rollback: Person=0 Profile=0
== Same, with the shared address persisted first
raw rows: Person=1 Profile=1 Address=1
-- setProfile(null); flush() (orphanRemoval)
sql> select p1_0.id,ba1_0.id,ba1_0.line1,p1_0.name,p2_0.id,p2_0.bio from Person p1_0 left join Address ba1_0 on ba1_0.id=p1_0.billingAddress_id left join Profile p2_0 on p2_0.id=p1_0.profile_id where p1_0.id=?
sql> update Person set billingAddress_id=?,name=?,profile_id=? where id=?
sql> delete from Profile where id=?
raw rows: Profile=0
-- remove(person); flush()
sql> select p1_0.id,ba1_0.id,ba1_0.line1,p1_0.name,p2_0.id,p2_0.bio from Person p1_0 left join Address ba1_0 on ba1_0.id=p1_0.billingAddress_id left join Profile p2_0 on p2_0.id=p1_0.profile_id where p1_0.id=?
sql> delete from Person where id=?
sql> delete from Profile where id=?
raw rows: Person=0 Profile=0 Address=1 (the address survives)
@@ -0,0 +1,23 @@
# @OneToOne: an owned profile cascades, a shared address does not
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
== Persisting a person with a new profile and a new, unsaved address
-- persist(person); flush()
sql> insert into Profile (bio,id) values (?,default)
sql> insert into Person (billingAddress_id,name,profile_id,id) values (?,?,?,default)
threw: java.lang.IllegalStateException <- org.hibernate.TransientPropertyValueException
cause: Persistent instance of 'com.ankurm.cascade.Person' references an unsaved transient instance of 'com.ankurm.cascade.Address' (persist the transient instance before flushing) [com.ankurm.cascade.Person.billingAddress -> com.ankurm.cascade.Address]
raw rows after rollback: Person=0 Profile=0
== Same, with the shared address persisted first
raw rows: Person=1 Profile=1 Address=1
-- setProfile(null); flush() (orphanRemoval)
sql> select p1_0.id,ba1_0.id,ba1_0.line1,p1_0.name,p2_0.id,p2_0.bio from Person p1_0 left join Address ba1_0 on ba1_0.id=p1_0.billingAddress_id left join Profile p2_0 on p2_0.id=p1_0.profile_id where p1_0.id=?
sql> update Person set billingAddress_id=?,name=?,profile_id=? where id=?
sql> delete from Profile where id=?
raw rows: Profile=0
-- remove(person); flush()
sql> select p1_0.id,ba1_0.id,ba1_0.line1,p1_0.name,p2_0.id,p2_0.bio from Person p1_0 left join Address ba1_0 on ba1_0.id=p1_0.billingAddress_id left join Profile p2_0 on p2_0.id=p1_0.profile_id where p1_0.id=?
sql> delete from Person where id=?
sql> delete from Profile where id=?
raw rows: Person=0 Profile=0 Address=1 (the address survives)
@@ -0,0 +1,24 @@
# @ManyToOne with cascade = {PERSIST, MERGE}: what it is for, and what it cannot do
# Hibernate ORM 7.4.15.Final, H2, JDK 25
== New parent created together with its first child
-- persist(new Ticket(.., new Agent())); flush()
sql> insert into Agent (name,id) values (?,default)
sql> insert into Ticket (agent_id,subject,id) values (?,?,default)
raw rows: Ticket=1 Agent=1
== Parent already exists (a detached Agent): persist vs merge
-- persist(new Ticket(.., detachedAgent)); flush()
(no SQL)
threw: jakarta.persistence.EntityExistsException <- org.hibernate.PersistentObjectException
cause: Detached entity passed to persist: com.ankurm.cascade.Agent
-- merge(new Ticket(.., detachedAgent)); flush()
sql> select a1_0.id,a1_0.name from Agent a1_0 where a1_0.id=?
sql> insert into Ticket (agent_id,subject,id) values (?,?,default)
raw rows: Ticket=2 Agent=2
== Removing a ticket never touches its agent
-- remove(ticket); flush()
sql> select t1_0.id,a1_0.id,a1_0.name,t1_0.subject from Ticket t1_0 left join Agent a1_0 on a1_0.id=t1_0.agent_id where t1_0.id=?
sql> delete from Ticket where id=?
raw rows: Ticket=1 Agent=2
@@ -0,0 +1,24 @@
# @ManyToOne with cascade = {PERSIST, MERGE}: what it is for, and what it cannot do
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
== New parent created together with its first child
-- persist(new Ticket(.., new Agent())); flush()
sql> insert into Agent (name,id) values (?,default)
sql> insert into Ticket (agent_id,subject,id) values (?,?,default)
raw rows: Ticket=1 Agent=1
== Parent already exists (a detached Agent): persist vs merge
-- persist(new Ticket(.., detachedAgent)); flush()
(no SQL)
threw: jakarta.persistence.EntityExistsException <- org.hibernate.PersistentObjectException
cause: Detached entity passed to persist: com.ankurm.cascade.Agent
-- merge(new Ticket(.., detachedAgent)); flush()
sql> select a1_0.id,a1_0.name from Agent a1_0 where a1_0.id=?
sql> insert into Ticket (agent_id,subject,id) values (?,?,default)
raw rows: Ticket=2 Agent=2
== Removing a ticket never touches its agent
-- remove(ticket); flush()
sql> select t1_0.id,a1_0.id,a1_0.name,t1_0.subject from Ticket t1_0 left join Agent a1_0 on a1_0.id=t1_0.agent_id where t1_0.id=?
sql> delete from Ticket where id=?
raw rows: Ticket=1 Agent=2
@@ -0,0 +1,26 @@
# Removing an invoice with 1000 lines: cascade REMOVE vs bulk delete first
# Hibernate ORM 7.4.15.Final, H2, JDK 25
effective hibernate.jdbc.batch_size: 15
== A. em.remove(invoice) with cascade = ALL, lines never touched by the code
entities in the persistence context after find(): 1
entities in the persistence context after remove(): 1001
entities deleted (Hibernate statistics): 1001
SQL strings prepared: total=1003 select=2 delete=1001
first statements:
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from InvoiceLine where id=?
sql> delete from InvoiceLine where id=?
== A2. the same removal with hibernate.flush.queue.type=legacy (ignored by 7.x)
SQL strings prepared: total=1003 select=2 delete=1001
== B. bulk delete of the lines first, then remove(getReference(invoice))
entities in the persistence context after remove(): 1
entities deleted (Hibernate statistics): 1
SQL strings prepared: total=4 select=2 delete=2
sql> delete from InvoiceLine il1_0 where il1_0.invoice_id=?
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from Invoice where id=?
@@ -0,0 +1,26 @@
# Removing an invoice with 1000 lines: cascade REMOVE vs bulk delete first
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
effective hibernate.jdbc.batch_size: 15
== A. em.remove(invoice) with cascade = ALL, lines never touched by the code
entities in the persistence context after find(): 1
entities in the persistence context after remove(): 1001
entities deleted (Hibernate statistics): 1001
SQL strings prepared: total=4 select=2 delete=2
first statements:
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from InvoiceLine where id=?
sql> delete from Invoice where id=?
== A2. the same removal with hibernate.flush.queue.type=legacy (ignored by 7.x)
SQL strings prepared: total=1003 select=2 delete=1001
== B. bulk delete of the lines first, then remove(getReference(invoice))
entities in the persistence context after remove(): 1
entities deleted (Hibernate statistics): 1
SQL strings prepared: total=4 select=2 delete=2
sql> delete from InvoiceLine il1_0 where il1_0.invoice_id=?
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from Invoice where id=?
@@ -0,0 +1,34 @@
# @OnDelete(CASCADE): the database deletes the children, Hibernate does not know
# Hibernate ORM 7.4.15.Final, H2, JDK 25
LedgerEntry foreign key: [FKJW461MHMPCBIN06DVK8AU5D6F | CASCADE]
VaultEntry foreign key: [FK84JU8V00N04VVRD9SKS3NCU3I | CASCADE]
== 1. Ledger: @OnDelete only, children never loaded
-- remove(find(Ledger)); flush()
sql> select l1_0.id from Ledger l1_0 where l1_0.id=?
sql> delete from Ledger where id=?
raw rows: Ledger=0 LedgerEntry=0
== 2. Ledger: @OnDelete only, but the entries are already loaded in the session
-- find(Ledger); entries.size(); remove(ledger); flush()
sql> select l1_0.id from Ledger l1_0 where l1_0.id=?
sql> select e1_0.ledger_id,e1_0.id,e1_0.memo from LedgerEntry e1_0 where e1_0.ledger_id=?
threw: java.lang.IllegalStateException <- org.hibernate.TransientPropertyValueException
cause: Persistent instance of 'com.ankurm.cascade.LedgerEntry' references an unsaved transient instance of 'com.ankurm.cascade.Ledger' (persist the transient instance before flushing) [com.ankurm.cascade.LedgerEntry.ledger -> com.ankurm.cascade.Ledger]
== 3. Vault: cascade = ALL plus @OnDelete, entries loaded
-- find(Vault); remove(vault); flush()
sql> delete from Vault where id=?
raw rows after commit: Vault=0 VaultEntry=0
same session, contains(firstEntry) after commit: false
same session, find(VaultEntry first): null
== 4. A cascade that only exists in the schema: the mapping (ProjectGood -> AppUser) says nothing about it
ProjectGood foreign key now: [FK_PROJECT_OWNER | CASCADE]
-- native: delete from AppUser where id = ?
sql> delete from AppUser where id = ?
raw rows: AppUser(id)=0 ProjectGood(id)=0
same session, find(ProjectGood) -> still returns the deleted project
SQL issued by that find: 0
same session, contains(project): true
after session.clear(), find(ProjectGood) -> null
@@ -0,0 +1,34 @@
# @OnDelete(CASCADE): the database deletes the children, Hibernate does not know
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
LedgerEntry foreign key: [FKJW461MHMPCBIN06DVK8AU5D6F | CASCADE]
VaultEntry foreign key: [FK84JU8V00N04VVRD9SKS3NCU3I | CASCADE]
== 1. Ledger: @OnDelete only, children never loaded
-- remove(find(Ledger)); flush()
sql> select l1_0.id from Ledger l1_0 where l1_0.id=?
sql> delete from Ledger where id=?
raw rows: Ledger=0 LedgerEntry=0
== 2. Ledger: @OnDelete only, but the entries are already loaded in the session
-- find(Ledger); entries.size(); remove(ledger); flush()
sql> select l1_0.id from Ledger l1_0 where l1_0.id=?
sql> select e1_0.ledger_id,e1_0.id,e1_0.memo from LedgerEntry e1_0 where e1_0.ledger_id=?
threw: java.lang.IllegalStateException <- org.hibernate.TransientPropertyValueException
cause: Persistent instance of 'com.ankurm.cascade.LedgerEntry' references an unsaved transient instance of 'com.ankurm.cascade.Ledger' (persist the transient instance before flushing) [com.ankurm.cascade.LedgerEntry.ledger -> com.ankurm.cascade.Ledger]
== 3. Vault: cascade = ALL plus @OnDelete, entries loaded
-- find(Vault); remove(vault); flush()
sql> delete from Vault where id=?
raw rows after commit: Vault=0 VaultEntry=0
same session, contains(firstEntry) after commit: false
same session, find(VaultEntry first): null
== 4. A cascade that only exists in the schema: the mapping (ProjectGood -> AppUser) says nothing about it
ProjectGood foreign key now: [FK_PROJECT_OWNER | CASCADE]
-- native: delete from AppUser where id = ?
sql> delete from AppUser where id = ?
raw rows: AppUser(id)=0 ProjectGood(id)=0
same session, find(ProjectGood) -> still returns the deleted project
SQL issued by that find: 0
same session, contains(project): true
after session.clear(), find(ProjectGood) -> null
@@ -0,0 +1,17 @@
# orphanRemoval: replacing the collection instead of changing it
# Hibernate ORM 7.4.15.Final, H2, JDK 25
== setLines(new ArrayList<>()) on a managed invoice
-- setLines(new ArrayList<>()); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
threw: org.hibernate.HibernateException
cause: A collection with orphan deletion was no longer referenced by the owning entity instance: com.ankurm.cascade.Invoice.lines
raw rows after rollback: InvoiceLine=2
== The safe way: clear() the existing collection
-- getLines().clear(); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from InvoiceLine where id=?
sql> delete from InvoiceLine where id=?
raw rows: InvoiceLine=0
@@ -0,0 +1,16 @@
# orphanRemoval: replacing the collection instead of changing it
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
== setLines(new ArrayList<>()) on a managed invoice
-- setLines(new ArrayList<>()); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
threw: org.hibernate.HibernateException
cause: A collection with orphan deletion was no longer referenced by the owning entity instance: com.ankurm.cascade.Invoice.lines
raw rows after rollback: InvoiceLine=2
== The safe way: clear() the existing collection
-- getLines().clear(); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from InvoiceLine where id=?
raw rows: InvoiceLine=0
@@ -0,0 +1,26 @@
# orphanRemoval and merge(): managed parent, emptied collection (reported as a 6.6 -> 7.x regression)
# Hibernate ORM 7.4.15.Final, H2, JDK 25
== 1. managed invoice, getLines().clear(), merge(invoice), flush()
-- clear(); merge(inv); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from InvoiceLine where id=?
sql> delete from InvoiceLine where id=?
raw rows: InvoiceLine=0
== 2. managed invoice, clear(), add one new line, merge(invoice), flush()
-- clear(); addLine(); merge(inv); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> insert into InvoiceLine (description,invoice_id,id) values (?,?,default)
sql> delete from InvoiceLine where id=?
sql> delete from InvoiceLine where id=?
raw rows for invoice M-2 lines: [replacement]
== 3. the normal detached round trip: load, close session, remove a line, merge in a new session
-- merge(detachedInvoice); flush()
sql> select i1_0.id,i1_0.number,l1_0.invoice_id,l1_0.id,l1_0.description from Invoice i1_0 left join InvoiceLine l1_0 on i1_0.id=l1_0.invoice_id where i1_0.id=?
sql> delete from InvoiceLine where id=?
raw rows for invoice M-3 lines: [2]
outcomes: 1=ok 2=ok 3=ok
@@ -0,0 +1,24 @@
# orphanRemoval and merge(): managed parent, emptied collection (reported as a 6.6 -> 7.x regression)
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
== 1. managed invoice, getLines().clear(), merge(invoice), flush()
-- clear(); merge(inv); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> delete from InvoiceLine where id=?
raw rows: InvoiceLine=0
== 2. managed invoice, clear(), add one new line, merge(invoice), flush()
-- clear(); addLine(); merge(inv); flush()
sql> select i1_0.id,i1_0.number from Invoice i1_0 where i1_0.id=?
sql> select l1_0.invoice_id,l1_0.id,l1_0.description from InvoiceLine l1_0 where l1_0.invoice_id=?
sql> insert into InvoiceLine (description,invoice_id,id) values (?,?,default)
sql> delete from InvoiceLine where id=?
raw rows for invoice M-2 lines: [replacement]
== 3. the normal detached round trip: load, close session, remove a line, merge in a new session
-- merge(detachedInvoice); flush()
sql> select i1_0.id,i1_0.number,l1_0.invoice_id,l1_0.id,l1_0.description from Invoice i1_0 left join InvoiceLine l1_0 on i1_0.id=l1_0.invoice_id where i1_0.id=?
sql> delete from InvoiceLine where id=?
raw rows for invoice M-3 lines: [2]
outcomes: 1=ok 2=ok 3=ok
@@ -0,0 +1,38 @@
# Deleting a parent and its (non-cascaded) child in one flush, parent removed first
# Hibernate ORM 7.4.15.Final, H2, JDK 25
== ProjectGood.owner is nullable
-- default settings
-- remove(parent); remove(child); flush()
sql> select au1_0.id,au1_0.name from AppUser au1_0 where au1_0.id=?
sql> select pg1_0.id,pg1_0.owner_id,pg1_0.title from ProjectGood pg1_0 where pg1_0.id=?
sql> update ProjectGood set owner_id=?,title=? where id=?
sql> delete from AppUser where id=?
sql> delete from ProjectGood where id=?
raw rows: parent=0 child=0
-- hibernate.flush.queue.type=legacy (a setting Hibernate 8 introduced; ignored by 7.x)
-- remove(parent); remove(child); flush()
sql> select au1_0.id,au1_0.name from AppUser au1_0 where au1_0.id=?
sql> select pg1_0.id,pg1_0.owner_id,pg1_0.title from ProjectGood pg1_0 where pg1_0.id=?
sql> update ProjectGood set owner_id=?,title=? where id=?
sql> delete from AppUser where id=?
sql> delete from ProjectGood where id=?
raw rows: parent=0 child=0
== Card.board is optional = false (the foreign key cannot be nulled)
-- default settings
-- remove(parent); remove(child); flush()
sql> select b1_0.id,b1_0.name from Board b1_0 where b1_0.id=?
sql> select c1_0.id,c1_0.board_id,b1_0.id,b1_0.name,c1_0.title from Card c1_0 join Board b1_0 on b1_0.id=c1_0.board_id where c1_0.id=?
threw: org.hibernate.PropertyValueException
cause: not-null property references a null or transient value for entity com.ankurm.cascade.Card.board
raw rows: parent=1 child=1
-- hibernate.flush.queue.type=legacy (a setting Hibernate 8 introduced; ignored by 7.x)
-- remove(parent); remove(child); flush()
sql> select b1_0.id,b1_0.name from Board b1_0 where b1_0.id=?
sql> select c1_0.id,c1_0.board_id,b1_0.id,b1_0.name,c1_0.title from Card c1_0 join Board b1_0 on b1_0.id=c1_0.board_id where c1_0.id=?
threw: org.hibernate.PropertyValueException
cause: not-null property references a null or transient value for entity com.ankurm.cascade.Card.board
raw rows: parent=1 child=1
outcomes: nullable default=ok/first delete: AppUser | nullable legacy=ok/first delete: AppUser | mandatory default=threw/first delete: none | mandatory legacy=threw/first delete: none
@@ -0,0 +1,38 @@
# Deleting a parent and its (non-cascaded) child in one flush, parent removed first
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
== ProjectGood.owner is nullable
-- default settings
-- remove(parent); remove(child); flush()
sql> select au1_0.id,au1_0.name from AppUser au1_0 where au1_0.id=?
sql> select pg1_0.id,pg1_0.owner_id,pg1_0.title from ProjectGood pg1_0 where pg1_0.id=?
sql> update ProjectGood set owner_id=?,title=? where id=?
sql> delete from ProjectGood where id=?
sql> delete from AppUser where id=?
raw rows: parent=0 child=0
-- hibernate.flush.queue.type=legacy (a setting Hibernate 8 introduced; ignored by 7.x)
-- remove(parent); remove(child); flush()
sql> select au1_0.id,au1_0.name from AppUser au1_0 where au1_0.id=?
sql> select pg1_0.id,pg1_0.owner_id,pg1_0.title from ProjectGood pg1_0 where pg1_0.id=?
sql> update ProjectGood set owner_id=?,title=? where id=?
sql> delete from AppUser where id=?
sql> delete from ProjectGood where id=?
raw rows: parent=0 child=0
== Card.board is optional = false (the foreign key cannot be nulled)
-- default settings
-- remove(parent); remove(child); flush()
sql> select b1_0.id,b1_0.name from Board b1_0 where b1_0.id=?
sql> select c1_0.id,c1_0.board_id,b1_0.id,b1_0.name,c1_0.title from Card c1_0 join Board b1_0 on b1_0.id=c1_0.board_id where c1_0.id=?
threw: org.hibernate.PropertyValueException
cause: not-null property references a null or transient value for entity com.ankurm.cascade.Card.board
raw rows: parent=1 child=1
-- hibernate.flush.queue.type=legacy (a setting Hibernate 8 introduced; ignored by 7.x)
-- remove(parent); remove(child); flush()
sql> select b1_0.id,b1_0.name from Board b1_0 where b1_0.id=?
sql> select c1_0.id,c1_0.board_id,b1_0.id,b1_0.name,c1_0.title from Card c1_0 join Board b1_0 on b1_0.id=c1_0.board_id where c1_0.id=?
threw: org.hibernate.PropertyValueException
cause: not-null property references a null or transient value for entity com.ankurm.cascade.Card.board
raw rows: parent=1 child=1
outcomes: nullable default=ok/first delete: ProjectGood | nullable legacy=ok/first delete: AppUser | mandatory default=threw/first delete: none | mandatory legacy=threw/first delete: none
@@ -0,0 +1,7 @@
# Which operation propagates along which cascade type (one @OneToOne per type, all to Leaf)
# Hibernate ORM 7.4.15.Final, H2, JDK 25
persist() propagates along: [onPersist, onAll]
merge() propagates along: [onMerge, onAll]
remove() propagates along: [onRemove, onAll]
refresh() propagates along: [onRefresh, onAll]
detach() propagates along: [onDetach, onAll]
@@ -0,0 +1,7 @@
# Which operation propagates along which cascade type (one @OneToOne per type, all to Leaf)
# Hibernate ORM 8.0.0.CR1, H2, JDK 25
persist() propagates along: [onPersist, onAll]
merge() propagates along: [onMerge, onAll]
remove() propagates along: [onRemove, onAll]
refresh() propagates along: [onRefresh, onAll]
detach() propagates along: [onDetach, onAll]