From 72d1e108037f1afc0e99d63b4fbf950b7510b47e Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Mon, 7 Aug 2023 11:45:05 +0200 Subject: [PATCH] HHH-16937 Handle optional `@Any` and discriminator/key nullability --- .../src/main/java/org/hibernate/annotations/Any.java | 4 ++++ .../java/org/hibernate/boot/model/internal/AnyBinder.java | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) 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() ); }