Skip to content

Commit

Permalink
HHH-12842 - Pass OneToOne constrained to determine nullability of type
Browse files Browse the repository at this point in the history
(cherry picked from commit 662f6b2)
  • Loading branch information
jwgmeligmeyling authored and gbadner committed Jul 15, 2021
1 parent d6bc532 commit f21e8c6
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 13 deletions.
Expand Up @@ -71,7 +71,8 @@ public Type getType() throws MappingException {
isLazy(),
isUnwrapProxy(),
entityName,
propertyName
propertyName,
constrained
);
}
else {
Expand All @@ -83,7 +84,8 @@ public Type getType() throws MappingException {
isLazy(),
isUnwrapProxy(),
entityName,
propertyName
propertyName,
constrained
);
}
}
Expand Down
28 changes: 25 additions & 3 deletions hibernate-core/src/main/java/org/hibernate/type/OneToOneType.java
Expand Up @@ -29,9 +29,10 @@ public class OneToOneType extends EntityType {
private final ForeignKeyDirection foreignKeyType;
private final String propertyName;
private final String entityName;
private final boolean constrained;

/**
* @deprecated Use {@link #OneToOneType(TypeFactory.TypeScope, String, ForeignKeyDirection, boolean, String, boolean, boolean, String, String)}
* @deprecated Use {@link #OneToOneType(TypeFactory.TypeScope, String, ForeignKeyDirection, boolean, String, boolean, boolean, String, String, boolean)}
* instead.
*/
@Deprecated
Expand All @@ -47,6 +48,11 @@ public OneToOneType(
this( scope, referencedEntityName, foreignKeyType, uniqueKeyPropertyName == null, uniqueKeyPropertyName, lazy, unwrapProxy, entityName, propertyName );
}

/**
* @deprecated Use {@link #OneToOneType(TypeFactory.TypeScope, String, ForeignKeyDirection, boolean, String, boolean, boolean, String, String, boolean)}
* instead.
*/
@Deprecated
public OneToOneType(
TypeFactory.TypeScope scope,
String referencedEntityName,
Expand All @@ -57,17 +63,33 @@ public OneToOneType(
boolean unwrapProxy,
String entityName,
String propertyName) {
this( scope, referencedEntityName, foreignKeyType, referenceToPrimaryKey, uniqueKeyPropertyName, lazy, unwrapProxy, entityName, propertyName, foreignKeyType != ForeignKeyDirection.TO_PARENT );
}

public OneToOneType(
TypeFactory.TypeScope scope,
String referencedEntityName,
ForeignKeyDirection foreignKeyType,
boolean referenceToPrimaryKey,
String uniqueKeyPropertyName,
boolean lazy,
boolean unwrapProxy,
String entityName,
String propertyName,
boolean constrained) {
super( scope, referencedEntityName, referenceToPrimaryKey, uniqueKeyPropertyName, !lazy, unwrapProxy );
this.foreignKeyType = foreignKeyType;
this.propertyName = propertyName;
this.entityName = entityName;
this.constrained = constrained;
}

public OneToOneType(OneToOneType original, String superTypeEntityName) {
super( original, superTypeEntityName );
this.foreignKeyType = original.foreignKeyType;
this.propertyName = original.propertyName;
this.entityName = original.entityName;
this.constrained = original.constrained;
}

@Override
Expand Down Expand Up @@ -155,8 +177,8 @@ public Object hydrate(
}

@Override
protected boolean isNullable() {
return foreignKeyType==ForeignKeyDirection.TO_PARENT;
public boolean isNullable() {
return !constrained;
}

@Override
Expand Down
Expand Up @@ -25,9 +25,10 @@
* @author Gavin King
*/
public class SpecialOneToOneType extends OneToOneType {

/**
* @deprecated Use {@link #SpecialOneToOneType(org.hibernate.type.TypeFactory.TypeScope, String, ForeignKeyDirection, boolean, String, boolean, boolean, String, String)} instead.
* @deprecated Use {@link SpecialOneToOneType#SpecialOneToOneType(TypeFactory.TypeScope, String, ForeignKeyDirection, boolean, String, boolean, boolean, String, String, boolean)}
* instead.
*/
@Deprecated
public SpecialOneToOneType(
Expand All @@ -41,17 +42,47 @@ public SpecialOneToOneType(
String propertyName) {
this( scope, referencedEntityName, foreignKeyType, uniqueKeyPropertyName == null, uniqueKeyPropertyName, lazy, unwrapProxy, entityName, propertyName );
}


/**
* @deprecated Use {@link SpecialOneToOneType#SpecialOneToOneType(TypeFactory.TypeScope, String, ForeignKeyDirection, boolean, String, boolean, boolean, String, String, boolean)}
* instead.
*/
@Deprecated
public SpecialOneToOneType(
TypeFactory.TypeScope scope,
String referencedEntityName,
ForeignKeyDirection foreignKeyType,
boolean referenceToPrimaryKey,
boolean referenceToPrimaryKey,
String uniqueKeyPropertyName,
boolean lazy,
boolean unwrapProxy,
String entityName,
String propertyName) {
this (
scope,
referencedEntityName,
foreignKeyType,
referenceToPrimaryKey,
uniqueKeyPropertyName,
lazy,
unwrapProxy,
entityName,
propertyName,
foreignKeyType != ForeignKeyDirection.TO_PARENT
);
}

public SpecialOneToOneType(
TypeFactory.TypeScope scope,
String referencedEntityName,
ForeignKeyDirection foreignKeyType,
boolean referenceToPrimaryKey,
String uniqueKeyPropertyName,
boolean lazy,
boolean unwrapProxy,
String entityName,
String propertyName,
boolean constrained) {
super(
scope,
referencedEntityName,
Expand All @@ -61,7 +92,8 @@ public SpecialOneToOneType(
lazy,
unwrapProxy,
entityName,
propertyName
propertyName,
constrained
);
}

Expand Down
43 changes: 40 additions & 3 deletions hibernate-core/src/main/java/org/hibernate/type/TypeFactory.java
Expand Up @@ -200,6 +200,11 @@ public static <T extends Serializable> SerializableType<T> serializable(Class<T>

// one-to-one type builders ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/**
* @deprecated Use {@link TypeFactory#oneToOne(String, ForeignKeyDirection, boolean, String, boolean, boolean, String, String, boolean)}
* instead.
*/
@Deprecated
public EntityType oneToOne(
String persistentClass,
ForeignKeyDirection foreignKeyType,
Expand All @@ -209,9 +214,39 @@ public EntityType oneToOne(
boolean unwrapProxy,
String entityName,
String propertyName) {
return oneToOne( persistentClass, foreignKeyType, referenceToPrimaryKey, uniqueKeyPropertyName, lazy, unwrapProxy, entityName, propertyName, foreignKeyType != ForeignKeyDirection.TO_PARENT );
}

/**
* @deprecated Use {@link TypeFactory#specialOneToOne(String, ForeignKeyDirection, boolean, String, boolean, boolean, String, String, boolean)}
* instead.
*/
@Deprecated
public EntityType specialOneToOne(
String persistentClass,
ForeignKeyDirection foreignKeyType,
boolean referenceToPrimaryKey,
String uniqueKeyPropertyName,
boolean lazy,
boolean unwrapProxy,
String entityName,
String propertyName) {
return specialOneToOne( persistentClass, foreignKeyType, referenceToPrimaryKey, uniqueKeyPropertyName, lazy, unwrapProxy, entityName, propertyName, foreignKeyType != ForeignKeyDirection.TO_PARENT );
}

public EntityType oneToOne(
String persistentClass,
ForeignKeyDirection foreignKeyType,
boolean referenceToPrimaryKey,
String uniqueKeyPropertyName,
boolean lazy,
boolean unwrapProxy,
String entityName,
String propertyName,
boolean constrained) {
return new OneToOneType(
typeScope, persistentClass, foreignKeyType, referenceToPrimaryKey,
uniqueKeyPropertyName, lazy, unwrapProxy, entityName, propertyName
uniqueKeyPropertyName, lazy, unwrapProxy, entityName, propertyName, constrained
);
}

Expand All @@ -223,10 +258,12 @@ public EntityType specialOneToOne(
boolean lazy,
boolean unwrapProxy,
String entityName,
String propertyName) {
String propertyName,
boolean constrained) {
return new SpecialOneToOneType(
typeScope, persistentClass, foreignKeyType, referenceToPrimaryKey,
uniqueKeyPropertyName, lazy, unwrapProxy, entityName, propertyName
uniqueKeyPropertyName, lazy, unwrapProxy, entityName, propertyName,
constrained
);
}

Expand Down

0 comments on commit f21e8c6

Please sign in to comment.