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<>(); } }