Skip to content

Commit 8bb7c25

Browse files
committed
HHH-19732 Fix wrong resetting of owned on-delete action when processing inverse collection
1 parent 37e1f9d commit 8bb7c25

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
@@ -44,6 +44,7 @@
4444
import org.hibernate.mapping.Selectable;
4545
import org.hibernate.mapping.SimpleValue;
4646
import org.hibernate.mapping.Table;
47+
import org.hibernate.mapping.ToOne;
4748
import org.hibernate.mapping.Value;
4849
import org.hibernate.metamodel.CollectionClassification;
4950
import org.hibernate.metamodel.UnsupportedMappingException;
@@ -2702,6 +2703,13 @@ private void bindUnownedManyToManyInverseForeignKey(
27022703
manyToOne.setReferencedPropertyName( referencedPropertyName );
27032704
metadataCollector.addUniquePropertyReference( targetEntity.getEntityName(), referencedPropertyName );
27042705
}
2706+
// Ensure that we copy over the delete action from the owner side before creating the foreign key
2707+
if ( property.getValue() instanceof Collection collectionValue ) {
2708+
manyToOne.setOnDeleteAction( ( (SimpleValue) collectionValue.getKey() ).getOnDeleteAction() );
2709+
}
2710+
else if ( property.getValue() instanceof ToOne toOne ) {
2711+
manyToOne.setOnDeleteAction( toOne.getOnDeleteAction() );
2712+
}
27052713
manyToOne.setReferenceToPrimaryKey( referencedPropertyName == null );
27062714
value.createForeignKey();
27072715
}

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)