throw exception for contradictory annotations

This commit is contained in:
Gavin 2022-11-21 16:41:09 +01:00 committed by Gavin King
parent 2e99811dd4
commit 2e02b9a74f
2 changed files with 21 additions and 12 deletions

View File

@ -94,35 +94,35 @@ public class OneToOneSecondPass implements SecondPass {
value.setPropertyName( propertyName ); value.setPropertyName( propertyName );
final String referencedEntityName = getReferenceEntityName( inferredData, targetEntity, buildingContext ); final String referencedEntityName = getReferenceEntityName( inferredData, targetEntity, buildingContext );
value.setReferencedEntityName( referencedEntityName ); value.setReferencedEntityName( referencedEntityName );
XProperty prop = inferredData.getProperty(); XProperty property = inferredData.getProperty();
ToOneBinder.defineFetchingStrategy( value, prop ); ToOneBinder.defineFetchingStrategy( value, property, inferredData, propertyHolder );
//value.setFetchMode( fetchMode ); //value.setFetchMode( fetchMode );
value.setCascadeDeleteEnabled( cascadeOnDelete ); value.setCascadeDeleteEnabled( cascadeOnDelete );
//value.setLazy( fetchMode != FetchMode.JOIN ); //value.setLazy( fetchMode != FetchMode.JOIN );
value.setConstrained( !optional ); value.setConstrained( !optional );
value.setForeignKeyType( getForeignKeyDirection() ); value.setForeignKeyType( getForeignKeyDirection() );
bindForeignKeyNameAndDefinition( value, prop, prop.getAnnotation( ForeignKey.class ), buildingContext ); bindForeignKeyNameAndDefinition( value, property, property.getAnnotation( ForeignKey.class ), buildingContext );
final PropertyBinder binder = new PropertyBinder(); final PropertyBinder binder = new PropertyBinder();
binder.setName( propertyName ); binder.setName( propertyName );
binder.setProperty(prop); binder.setProperty( property );
binder.setValue( value ); binder.setValue( value );
binder.setCascade( cascadeStrategy ); binder.setCascade( cascadeStrategy );
binder.setAccessType( inferredData.getDefaultAccess() ); binder.setAccessType( inferredData.getDefaultAccess() );
final LazyGroup lazyGroupAnnotation = prop.getAnnotation( LazyGroup.class ); final LazyGroup lazyGroupAnnotation = property.getAnnotation( LazyGroup.class );
if ( lazyGroupAnnotation != null ) { if ( lazyGroupAnnotation != null ) {
binder.setLazyGroup( lazyGroupAnnotation.value() ); binder.setLazyGroup( lazyGroupAnnotation.value() );
} }
final Property property = binder.makeProperty(); final Property result = binder.makeProperty();
property.setOptional( optional ); result.setOptional( optional );
if ( isEmptyAnnotationValue( mappedBy ) ) { if ( isEmptyAnnotationValue( mappedBy ) ) {
bindUnowned( persistentClasses, value, propertyName, property ); bindUnowned( persistentClasses, value, propertyName, result );
} }
else { else {
bindOwned( persistentClasses, value, property ); bindOwned( persistentClasses, value, result );
} }
value.sortProperties(); value.sortProperties();
} }

View File

@ -148,7 +148,7 @@ public class ToOneBinder {
} }
value.setReferencedEntityName( getReferenceEntityName( inferredData, targetEntity, context ) ); value.setReferencedEntityName( getReferenceEntityName( inferredData, targetEntity, context ) );
final XProperty property = inferredData.getProperty(); final XProperty property = inferredData.getProperty();
defineFetchingStrategy( value, property ); defineFetchingStrategy( value, property, inferredData, propertyHolder );
//value.setFetchMode( fetchMode ); //value.setFetchMode( fetchMode );
value.setNotFoundAction( notFoundAction ); value.setNotFoundAction( notFoundAction );
value.setCascadeDeleteEnabled( cascadeOnDelete ); value.setCascadeDeleteEnabled( cascadeOnDelete );
@ -295,7 +295,11 @@ public class ToOneBinder {
} }
} }
static void defineFetchingStrategy(ToOne toOne, XProperty property) { static void defineFetchingStrategy(
ToOne toOne,
XProperty property,
PropertyData inferredData,
PropertyHolder propertyHolder) {
final FetchType fetchType = getJpaFetchType( property ); final FetchType fetchType = getJpaFetchType( property );
final LazyToOne lazy = property.getAnnotation( LazyToOne.class ); final LazyToOne lazy = property.getAnnotation( LazyToOne.class );
@ -305,7 +309,12 @@ public class ToOneBinder {
toOne.setUnwrapProxy( true ); toOne.setUnwrapProxy( true );
} }
else if ( lazy != null ) { else if ( lazy != null ) {
toOne.setLazy( lazy.value() != LazyToOneOption.FALSE ); boolean lazyFalse = lazy.value() == LazyToOneOption.FALSE;
if ( fetchType == FetchType.LAZY && lazyFalse ) {
throw new AnnotationException("Association '" + getPath( propertyHolder, inferredData )
+ "' is marked 'fetch=LAZY' and '@LazyToOne(FALSE)'");
}
toOne.setLazy( !lazyFalse );
toOne.setUnwrapProxy( lazy.value() == LazyToOneOption.NO_PROXY ); toOne.setUnwrapProxy( lazy.value() == LazyToOneOption.NO_PROXY );
} }
else { else {