Skip to content

Commit 61c52f8

Browse files
committed
HHH-19732 Fix wrong resetting of owned on-delete action when processing inverse collection
1 parent 42c6185 commit 61c52f8

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.hibernate.mapping.Selectable;
5151
import org.hibernate.mapping.SimpleValue;
5252
import org.hibernate.mapping.Table;
53+
import org.hibernate.mapping.ToOne;
5354
import org.hibernate.mapping.Value;
5455
import org.hibernate.metamodel.CollectionClassification;
5556
import org.hibernate.metamodel.UnsupportedMappingException;
@@ -2747,6 +2748,13 @@ private void bindUnownedManyToManyInverseForeignKey(
27472748
manyToOne.setReferencedPropertyName( referencedPropertyName );
27482749
metadataCollector.addUniquePropertyReference( targetEntity.getEntityName(), referencedPropertyName );
27492750
}
2751+
// Ensure that we copy over the delete action from the owner side before creating the foreign key
2752+
if ( property.getValue() instanceof Collection collectionValue ) {
2753+
manyToOne.setOnDeleteAction( ( (SimpleValue) collectionValue.getKey() ).getOnDeleteAction() );
2754+
}
2755+
else {
2756+
manyToOne.setOnDeleteAction( ( (ToOne) property.getValue() ).getOnDeleteAction() );
2757+
}
27502758
manyToOne.setReferenceToPrimaryKey( referencedPropertyName == null );
27512759
value.createForeignKey();
27522760
}

hibernate-core/src/test/java/org/hibernate/orm/test/ondeletecascade/OnDeleteManyToManyTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,8 @@ static class A {
8888
static class B {
8989
@Id
9090
long id;
91+
@ManyToMany(mappedBy = "bs")
92+
@OnDelete(action = OnDeleteAction.CASCADE)
93+
Set<A> as = new HashSet<>();
9194
}
9295
}

0 commit comments

Comments
 (0)