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;