From ec737a7f15fe80cb53f949e2fa6c67cfbe7a1bb7 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 3 Mar 2022 15:45:12 -0600 Subject: [PATCH] HHH-15099 - Improve handling of associations marked with @NotFound - Disable physical foreign-key export for `@NotFound` mappings --- .../org/hibernate/cfg/AnnotationBinder.java | 18 ++++++++++++++---- .../notfound/NotFoundLogicalOneToOneTest.java | 15 +++++---------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 079f34ece0..8392cda2af 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -3961,10 +3961,20 @@ public final class AnnotationBinder { JoinColumns joinColumns, MetadataBuildingContext context) { final boolean noConstraintByDefault = context.getBuildingOptions().isNoConstraintByDefault(); - if ( ( joinColumn != null && ( joinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT - || joinColumn.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault ) ) - || ( joinColumns != null && ( joinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT - || joinColumns.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault ) ) ) { + final NotFound notFoundAnn= property.getAnnotation( NotFound.class ); + + if ( notFoundAnn != null ) { + // supersedes all others + value.disableForeignKey(); + } + else if ( joinColumn != null && ( + joinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT + || ( joinColumn.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault ) ) ) { + value.disableForeignKey(); + } + else if ( joinColumns != null && ( + joinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT + || ( joinColumns.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault ) ) ) { value.disableForeignKey(); } else { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/notfound/NotFoundLogicalOneToOneTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/notfound/NotFoundLogicalOneToOneTest.java index a4a2372875..2545d8f597 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/notfound/NotFoundLogicalOneToOneTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/notfound/NotFoundLogicalOneToOneTest.java @@ -7,6 +7,11 @@ package org.hibernate.orm.test.annotations.notfound; import java.io.Serializable; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.OneToOne; import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; @@ -16,15 +21,6 @@ import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.junit.jupiter.api.Test; -import jakarta.persistence.ConstraintMode; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.ForeignKey; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.OneToOne; - import static org.junit.jupiter.api.Assertions.assertNull; /** @@ -92,7 +88,6 @@ public class NotFoundLogicalOneToOneTest { } @OneToOne(fetch = FetchType.LAZY) - @JoinColumn(foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT)) @NotFound(action = NotFoundAction.IGNORE) public Currency getCurrency() { return currency;