From 0709de5e09f53c1c93ea37823d1d4b6e01be24e7 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Thu, 21 Aug 2025 17:42:36 +0200 Subject: [PATCH] HHH-19732 Fix wrong resetting of owned on-delete action when processing inverse collection --- .../hibernate/boot/model/internal/CollectionBinder.java | 8 ++++++++ .../orm/test/ondeletecascade/OnDeleteManyToManyTest.java | 3 +++ 2 files changed, 11 insertions(+) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java index 229646b5bf95..406e643a19e4 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java @@ -50,6 +50,7 @@ import org.hibernate.mapping.Selectable; import org.hibernate.mapping.SimpleValue; import org.hibernate.mapping.Table; +import org.hibernate.mapping.ToOne; import org.hibernate.mapping.Value; import org.hibernate.metamodel.CollectionClassification; import org.hibernate.metamodel.UnsupportedMappingException; @@ -2747,6 +2748,13 @@ private void bindUnownedManyToManyInverseForeignKey( manyToOne.setReferencedPropertyName( referencedPropertyName ); metadataCollector.addUniquePropertyReference( targetEntity.getEntityName(), referencedPropertyName ); } + // Ensure that we copy over the delete action from the owner side before creating the foreign key + if ( property.getValue() instanceof Collection collectionValue ) { + manyToOne.setOnDeleteAction( ( (SimpleValue) collectionValue.getKey() ).getOnDeleteAction() ); + } + else if ( property.getValue() instanceof ToOne toOne ) { + manyToOne.setOnDeleteAction( toOne.getOnDeleteAction() ); + } manyToOne.setReferenceToPrimaryKey( referencedPropertyName == null ); value.createForeignKey(); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/ondeletecascade/OnDeleteManyToManyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/ondeletecascade/OnDeleteManyToManyTest.java index fc383d32ea76..fd294482511b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/ondeletecascade/OnDeleteManyToManyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/ondeletecascade/OnDeleteManyToManyTest.java @@ -88,5 +88,8 @@ static class A { static class B { @Id long id; + @ManyToMany(mappedBy = "bs") + @OnDelete(action = OnDeleteAction.CASCADE) + Set as = new HashSet<>(); } }