Skip to content

Commit e5b2298

Browse files
dreab8beikov
authored andcommitted
HHH-17290 Embeddable with a primitive field cannot be set to null
1 parent 5830151 commit e5b2298

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumn.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,7 @@ private static AnnotatedColumns buildImplicitColumn(
881881
// }
882882
//not following the spec but more clean
883883
if ( nullability != Nullability.FORCED_NULL
884-
&& inferredData.getClassOrElement().isPrimitive()
885-
&& !inferredData.getProperty().isArray() ) {
884+
&& !PropertyBinder.isOptional( inferredData.getProperty(), propertyHolder ) ) {
886885
column.setNullable( false );
887886
}
888887
final String propertyName = inferredData.getPropertyName();

hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ private void handleMutability(Property property) {
451451

452452
private void handleOptional(Property property) {
453453
if ( this.property != null ) {
454-
property.setOptional( !isId && isOptional( this.property ) );
454+
property.setOptional( !isId && isOptional( this.property, this.holder ) );
455455
if ( property.isOptional() ) {
456456
final OptionalDeterminationSecondPass secondPass = persistentClasses -> {
457457
// Defer determining whether a property and its columns are nullable,
@@ -1200,10 +1200,12 @@ private static boolean isExplicitlyOptional(XProperty property) {
12001200
* Should this property be considered optional, taking into
12011201
* account whether it is primitive?
12021202
*/
1203-
private static boolean isOptional(XProperty property) {
1203+
public static boolean isOptional(XProperty property, PropertyHolder propertyHolder) {
12041204
return property.isAnnotationPresent( Basic.class )
12051205
? property.getAnnotation( Basic.class ).optional()
1206-
: property.isArray() || !property.getClassOrElementClass().isPrimitive();
1206+
: property.isArray()
1207+
|| propertyHolder != null && propertyHolder.isComponent()
1208+
|| !property.getClassOrElementClass().isPrimitive();
12071209
}
12081210

12091211
private static boolean isLazy(XProperty property) {

hibernate-core/src/main/java/org/hibernate/type/ComponentType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ public Object replace(
533533
Object owner,
534534
Map<Object, Object> copyCache) {
535535

536-
if ( original == null && target == null ) {
536+
if ( original == null ) {
537537
return null;
538538
}
539539
if ( compositeUserType != null ) {
@@ -570,7 +570,7 @@ public Object replace(
570570
Map<Object, Object> copyCache,
571571
ForeignKeyDirection foreignKeyDirection) {
572572

573-
if ( original == null && target == null ) {
573+
if ( original == null ) {
574574
return null;
575575
}
576576
if ( compositeUserType != null ) {

0 commit comments

Comments
 (0)