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

View File

@ -148,7 +148,7 @@ public class ToOneBinder {
}
value.setReferencedEntityName( getReferenceEntityName( inferredData, targetEntity, context ) );
final XProperty property = inferredData.getProperty();
defineFetchingStrategy( value, property );
defineFetchingStrategy( value, property, inferredData, propertyHolder );
//value.setFetchMode( fetchMode );
value.setNotFoundAction( notFoundAction );
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 LazyToOne lazy = property.getAnnotation( LazyToOne.class );
@ -305,7 +309,12 @@ public class ToOneBinder {
toOne.setUnwrapProxy( true );
}
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 );
}
else {