From e5b2298b767c19cf6d9ae9df76a723c8af58b936 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 5 Oct 2023 17:36:28 +0200 Subject: [PATCH] HHH-17290 Embeddable with a primitive field cannot be set to null --- .../hibernate/boot/model/internal/AnnotatedColumn.java | 3 +-- .../org/hibernate/boot/model/internal/PropertyBinder.java | 8 +++++--- .../src/main/java/org/hibernate/type/ComponentType.java | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumn.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumn.java index 02beed2fff..097fe7c36d 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumn.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumn.java @@ -881,8 +881,7 @@ public class AnnotatedColumn { // } //not following the spec but more clean if ( nullability != Nullability.FORCED_NULL - && inferredData.getClassOrElement().isPrimitive() - && !inferredData.getProperty().isArray() ) { + && !PropertyBinder.isOptional( inferredData.getProperty(), propertyHolder ) ) { column.setNullable( false ); } final String propertyName = inferredData.getPropertyName(); diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java index 4e8e1b3c87..a73fbb07b0 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java @@ -451,7 +451,7 @@ public class PropertyBinder { private void handleOptional(Property property) { if ( this.property != null ) { - property.setOptional( !isId && isOptional( this.property ) ); + property.setOptional( !isId && isOptional( this.property, this.holder ) ); if ( property.isOptional() ) { final OptionalDeterminationSecondPass secondPass = persistentClasses -> { // Defer determining whether a property and its columns are nullable, @@ -1200,10 +1200,12 @@ public class PropertyBinder { * Should this property be considered optional, taking into * account whether it is primitive? */ - private static boolean isOptional(XProperty property) { + public static boolean isOptional(XProperty property, PropertyHolder propertyHolder) { return property.isAnnotationPresent( Basic.class ) ? property.getAnnotation( Basic.class ).optional() - : property.isArray() || !property.getClassOrElementClass().isPrimitive(); + : property.isArray() + || propertyHolder != null && propertyHolder.isComponent() + || !property.getClassOrElementClass().isPrimitive(); } private static boolean isLazy(XProperty property) { diff --git a/hibernate-core/src/main/java/org/hibernate/type/ComponentType.java b/hibernate-core/src/main/java/org/hibernate/type/ComponentType.java index 90168313e7..d6b070db32 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/ComponentType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/ComponentType.java @@ -533,7 +533,7 @@ public class ComponentType extends AbstractType implements CompositeTypeImplemen Object owner, Map copyCache) { - if ( original == null && target == null ) { + if ( original == null ) { return null; } if ( compositeUserType != null ) { @@ -570,7 +570,7 @@ public class ComponentType extends AbstractType implements CompositeTypeImplemen Map copyCache, ForeignKeyDirection foreignKeyDirection) { - if ( original == null && target == null ) { + if ( original == null ) { return null; } if ( compositeUserType != null ) {