Skip to content

Commit

Permalink
HHH-12436 - Attempted to assign id from null one-to-one property
Browse files Browse the repository at this point in the history
(cherry picked from commit 3218f6c)
  • Loading branch information
gbadner committed Jul 15, 2021
1 parent f0117e8 commit b419a00
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
Expand Up @@ -1764,7 +1764,16 @@ private static void processElementAnnotations(
joinColumn.setExplicitTableName( join.getTable().getName() );
}
}
final boolean mandatory = !ann.optional() || forcePersist;
// MapsId means the columns belong to the pk;
// A @MapsId association (obviously) must be non-null when the entity is first persisted.
// If a @MapsId association is not mapped with @NotFound(IGNORE), then the association
// is mandatory (even if the association has optional=true).
// If a @MapsId association has optional=true and is mapped with @NotFound(IGNORE) then
// the association is optional.
final boolean mandatory =
!ann.optional() ||
property.isAnnotationPresent( Id.class ) ||
( property.isAnnotationPresent( MapsId.class ) && !ignoreNotFound );
bindManyToOne(
getCascadeStrategy( ann.cascade(), hibernateCascade, false, forcePersist ),
joinColumns,
Expand Down Expand Up @@ -1794,11 +1803,23 @@ else if ( property.isAnnotationPresent( OneToOne.class ) ) {
}

//FIXME support a proper PKJCs
boolean trueOneToOne = property.isAnnotationPresent( PrimaryKeyJoinColumn.class )
final boolean hasPkjc = property.isAnnotationPresent( PrimaryKeyJoinColumn.class )
|| property.isAnnotationPresent( PrimaryKeyJoinColumns.class );
boolean trueOneToOne = hasPkjc;
Cascade hibernateCascade = property.getAnnotation( Cascade.class );
NotFound notFound = property.getAnnotation( NotFound.class );
boolean ignoreNotFound = notFound != null && notFound.action().equals( NotFoundAction.IGNORE );
// MapsId means the columns belong to the pk;
// A @MapsId association (obviously) must be non-null when the entity is first persisted.
// If a @MapsId association is not mapped with @NotFound(IGNORE), then the association
// is mandatory (even if the association has optional=true).
// If a @MapsId association has optional=true and is mapped with @NotFound(IGNORE) then
// the association is optional.
// @OneToOne(optional = true) with @PKJC makes the association optional.
final boolean mandatory =
!ann.optional() ||
property.isAnnotationPresent( Id.class ) ||
( property.isAnnotationPresent( MapsId.class ) && !ignoreNotFound );
OnDelete onDeleteAnn = property.getAnnotation( OnDelete.class );
boolean onDeleteCascade = onDeleteAnn != null && OnDeleteAction.CASCADE.equals( onDeleteAnn.action() );
JoinTable assocTable = propertyHolder.getJoinTable( property );
Expand All @@ -1808,9 +1829,6 @@ else if ( property.isAnnotationPresent( OneToOne.class ) ) {
joinColumn.setExplicitTableName( join.getTable().getName() );
}
}
//MapsId means the columns belong to the pk => not null
//@OneToOne with @PKJC can still be optional
final boolean mandatory = !ann.optional() || forcePersist;
bindOneToOne(
getCascadeStrategy( ann.cascade(), hibernateCascade, ann.orphanRemoval(), forcePersist ),
joinColumns,
Expand Down
Expand Up @@ -92,20 +92,11 @@ public void doSecondPass(Map persistentClasses) throws MappingException {
value.setCascadeDeleteEnabled( cascadeOnDelete );
//value.setLazy( fetchMode != FetchMode.JOIN );

if ( !optional ) {
value.setConstrained( true );
}
if ( value.isReferenceToPrimaryKey() ) {
value.setForeignKeyType( ForeignKeyDirection.TO_PARENT );
}
else {
value.setForeignKeyType(
value.isConstrained()
? ForeignKeyDirection.FROM_PARENT
: ForeignKeyDirection.TO_PARENT
);
}

value.setConstrained( !optional );
final ForeignKeyDirection foreignKeyDirection = !BinderHelper.isEmptyAnnotationValue( mappedBy )
? ForeignKeyDirection.TO_PARENT
: ForeignKeyDirection.FROM_PARENT;
value.setForeignKeyType(foreignKeyDirection);
AnnotationBinder.bindForeignKeyNameAndDefinition(
value,
inferredData.getProperty(),
Expand Down

0 comments on commit b419a00

Please sign in to comment.