HHH-16908 Mapping error when using unowned associations as identifiers

This commit is contained in:
Marco Belladelli 2023-07-19 14:52:16 +02:00 committed by Christian Beikov
parent 6bc0de2a33
commit 8f74d6c2f0
2 changed files with 24 additions and 3 deletions

View File

@ -208,7 +208,7 @@ public class OneToOneSecondPass implements SecondPass {
final KeyValue targetEntityIdentifier = targetEntity.getIdentifier();
boolean referenceToPrimaryKey = mappedBy == null
|| targetEntityIdentifier instanceof Component
&& !( (Component) targetEntityIdentifier ).hasProperty( mappedBy );
&& ( (Component) targetEntityIdentifier ).hasProperty( mappedBy );
oneToOne.setReferenceToPrimaryKey( referenceToPrimaryKey );
final String propertyRef = oneToOne.getReferencedPropertyName();

View File

@ -92,6 +92,13 @@ public class ToOneBinder {
);
}
if ( joinColumns.hasMappedBy() && isIdentifier( propertyHolder, propertyBinder, isIdentifierMapper ) ) {
throw new AnnotationException(
"Property '" + getPath( propertyHolder, inferredData )
+ "' is the inverse side of a '@ManyToOne' association and cannot be used as identifier"
);
}
final Cascade hibernateCascade = property.getAnnotation( Cascade.class );
final NotFound notFound = property.getAnnotation( NotFound.class );
final NotFoundAction notFoundAction = notFound == null ? null : notFound.action();
@ -121,6 +128,13 @@ public class ToOneBinder {
);
}
private static boolean isIdentifier(
PropertyHolder propertyHolder,
PropertyBinder propertyBinder,
boolean isIdentifierMapper) {
return propertyBinder.isId() || propertyHolder.isOrWithinEmbeddedId() || propertyHolder.isInIdClass() || isIdentifierMapper;
}
private static boolean isMandatory(boolean optional, XProperty property, NotFoundAction notFoundAction) {
// @MapsId means the columns belong to the pk;
// A @MapsId association (obviously) must be non-null when the entity is first persisted.
@ -405,6 +419,13 @@ public class ToOneBinder {
);
}
if ( joinColumns.hasMappedBy() && isIdentifier( propertyHolder, propertyBinder, isIdentifierMapper ) ) {
throw new AnnotationException(
"Property '" + getPath( propertyHolder, inferredData )
+ "' is the inverse side of a '@OneToOne' association and cannot be used as identifier"
);
}
//FIXME support a proper PKJCs
final boolean trueOneToOne = property.isAnnotationPresent( PrimaryKeyJoinColumn.class )
|| property.isAnnotationPresent( PrimaryKeyJoinColumns.class );