From 058586135164a5ddcbd64b09ff56ff887b238d2d 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 4d7baad63e1b..3009140d10ff 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 @@ -44,6 +44,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; @@ -2702,6 +2703,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<>(); } }