diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/Any.java b/hibernate-core/src/main/java/org/hibernate/annotations/Any.java index 6ecf6fe6a8..242ad2da2c 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/Any.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/Any.java @@ -102,6 +102,10 @@ public @interface Any { /** * Whether the association is optional. + *
+ * If the discriminator {@link jakarta.persistence.Column Column} or the + * {@link jakarta.persistence.JoinColumn JoinColumn} are not nullable the + * association is always considered non-optional, regardless of this value. * * @return {@code false} if the association cannot be null. */ diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnyBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnyBinder.java index 44243b9f62..b8742a3bd4 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnyBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnyBinder.java @@ -94,6 +94,7 @@ public class AnyBinder { } final boolean lazy = any.fetch() == FetchType.LAZY; + final boolean optional = any.optional(); final Any value = BinderHelper.buildAnyValue( property.getAnnotation( Column.class ), getOverridableAnnotation( property, Formula.class, context ), @@ -104,7 +105,7 @@ public class AnyBinder { nullability, propertyHolder, entityBinder, - any.optional(), + optional, context ); @@ -121,6 +122,7 @@ public class AnyBinder { binder.setCascade( cascadeStrategy ); binder.setBuildingContext( context ); Property prop = binder.makeProperty(); + prop.setOptional( optional && value.isNullable() ); //composite FK columns are in the same table, so it's OK propertyHolder.addProperty( prop, columns, inferredData.getDeclaringClass() ); }