HHH-16937 Handle optional `@Any` and discriminator/key nullability

This commit is contained in:
Marco Belladelli 2023-08-07 11:45:05 +02:00
parent b99d914d14
commit c72efeb92d
No known key found for this signature in database
GPG Key ID: D1D0C3030AE3AA35
2 changed files with 7 additions and 1 deletions

View File

@ -101,6 +101,10 @@ public @interface Any {
/** /**
* Whether the association is optional. * Whether the association is optional.
* <p>
* 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. * @return {@code false} if the association cannot be null.
*/ */

View File

@ -94,6 +94,7 @@ public class AnyBinder {
} }
final boolean lazy = any.fetch() == FetchType.LAZY; final boolean lazy = any.fetch() == FetchType.LAZY;
final boolean optional = any.optional();
final Any value = BinderHelper.buildAnyValue( final Any value = BinderHelper.buildAnyValue(
property.getAnnotation( Column.class ), property.getAnnotation( Column.class ),
getOverridableAnnotation( property, Formula.class, context ), getOverridableAnnotation( property, Formula.class, context ),
@ -104,7 +105,7 @@ public class AnyBinder {
nullability, nullability,
propertyHolder, propertyHolder,
entityBinder, entityBinder,
any.optional(), optional,
context context
); );
@ -121,6 +122,7 @@ public class AnyBinder {
binder.setCascade( cascadeStrategy ); binder.setCascade( cascadeStrategy );
binder.setBuildingContext( context ); binder.setBuildingContext( context );
Property prop = binder.makeProperty(); Property prop = binder.makeProperty();
prop.setOptional( optional && value.isNullable() );
//composite FK columns are in the same table, so it's OK //composite FK columns are in the same table, so it's OK
propertyHolder.addProperty( prop, columns, inferredData.getDeclaringClass() ); propertyHolder.addProperty( prop, columns, inferredData.getDeclaringClass() );
} }