diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 0128bf3143..ac549617f7 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -3040,37 +3040,13 @@ public final class AnnotationBinder { final String propertyName = inferredData.getPropertyName(); value.setTypeUsingReflection( propertyHolder.getClassName(), propertyName ); - if ( ( joinColumn != null && joinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT ) - || ( joinColumns != null && joinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT ) ) { - // not ideal... - value.setForeignKeyName( "none" ); - } - else { - final ForeignKey fk = property.getAnnotation( ForeignKey.class ); - if ( fk != null && StringHelper.isNotEmpty( fk.name() ) ) { - value.setForeignKeyName( fk.name() ); - } - else { - final javax.persistence.ForeignKey fkOverride = propertyHolder.getOverriddenForeignKey( - StringHelper.qualify( propertyHolder.getPath(), propertyName ) - ); - if ( fkOverride != null && fkOverride.value() == ConstraintMode.NO_CONSTRAINT ) { - value.setForeignKeyName( "none" ); - } - else if ( fkOverride != null ) { - value.setForeignKeyName( StringHelper.nullIfEmpty( fkOverride.name() ) ); - value.setForeignKeyDefinition( StringHelper.nullIfEmpty( fkOverride.foreignKeyDefinition() ) ); - } - else if ( joinColumns != null ) { - value.setForeignKeyName( StringHelper.nullIfEmpty( joinColumns.foreignKey().name() ) ); - value.setForeignKeyDefinition( StringHelper.nullIfEmpty( joinColumns.foreignKey().foreignKeyDefinition() ) ); - } - else if ( joinColumn != null ) { - value.setForeignKeyName( StringHelper.nullIfEmpty( joinColumn.foreignKey().name() ) ); - value.setForeignKeyDefinition( StringHelper.nullIfEmpty( joinColumn.foreignKey().foreignKeyDefinition() ) ); - } - } - } + bindForeignKeyNameAndDefinition( + value, + property, + propertyHolder.getOverriddenForeignKey( StringHelper.qualify( propertyHolder.getPath(), propertyName ) ), + joinColumn, + joinColumns + ); String path = propertyHolder.getPath() + "." + propertyName; FkSecondPass secondPass = new ToOneFkSecondPass( @@ -3401,6 +3377,41 @@ public final class AnnotationBinder { } } + public static void bindForeignKeyNameAndDefinition( + SimpleValue value, + XProperty property, + javax.persistence.ForeignKey fkOverride, + JoinColumn joinColumn, + JoinColumns joinColumns) { + if ( ( joinColumn != null && joinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT ) + || ( joinColumns != null && joinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT ) ) { + value.setForeignKeyName( "none" ); + } + else { + final ForeignKey fk = property.getAnnotation( ForeignKey.class ); + if ( fk != null && StringHelper.isNotEmpty( fk.name() ) ) { + value.setForeignKeyName( fk.name() ); + } + else { + if ( fkOverride != null && fkOverride.value() == ConstraintMode.NO_CONSTRAINT ) { + value.setForeignKeyName( "none" ); + } + else if ( fkOverride != null ) { + value.setForeignKeyName( StringHelper.nullIfEmpty( fkOverride.name() ) ); + value.setForeignKeyDefinition( StringHelper.nullIfEmpty( fkOverride.foreignKeyDefinition() ) ); + } + else if ( joinColumns != null ) { + value.setForeignKeyName( StringHelper.nullIfEmpty( joinColumns.foreignKey().name() ) ); + value.setForeignKeyDefinition( StringHelper.nullIfEmpty( joinColumns.foreignKey().foreignKeyDefinition() ) ); + } + else if ( joinColumn != null ) { + value.setForeignKeyName( StringHelper.nullIfEmpty( joinColumn.foreignKey().name() ) ); + value.setForeignKeyDefinition( StringHelper.nullIfEmpty( joinColumn.foreignKey().foreignKeyDefinition() ) ); + } + } + } + } + private static HashMap buildGenerators(XAnnotatedElement annElt, MetadataBuildingContext context) { InFlightMetadataCollector metadataCollector = context.getMetadataCollector(); HashMap generators = new HashMap<>(); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/OneToOneSecondPass.java b/hibernate-core/src/main/java/org/hibernate/cfg/OneToOneSecondPass.java index 7a1f3baa7b..24d6b7a7f1 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/OneToOneSecondPass.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/OneToOneSecondPass.java @@ -8,11 +8,12 @@ package org.hibernate.cfg; import java.util.Iterator; import java.util.Map; -import javax.persistence.ConstraintMode; + +import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; import org.hibernate.AnnotationException; import org.hibernate.MappingException; -import org.hibernate.annotations.ForeignKey; import org.hibernate.annotations.LazyGroup; import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.boot.spi.MetadataBuildingContext; @@ -104,6 +105,15 @@ public class OneToOneSecondPass implements SecondPass { : ForeignKeyDirection.TO_PARENT ); } + + AnnotationBinder.bindForeignKeyNameAndDefinition( + value, + inferredData.getProperty(), + inferredData.getProperty().getAnnotation( javax.persistence.ForeignKey.class ), + inferredData.getProperty().getAnnotation( JoinColumn.class ), + inferredData.getProperty().getAnnotation( JoinColumns.class ) + ); + PropertyBinder binder = new PropertyBinder(); binder.setName( propertyName ); binder.setValue( value ); @@ -259,23 +269,6 @@ public class OneToOneSecondPass implements SecondPass { ); } } - - final ForeignKey fk = inferredData.getProperty().getAnnotation( ForeignKey.class ); - if ( fk != null && !BinderHelper.isEmptyAnnotationValue( fk.name() ) ) { - value.setForeignKeyName( fk.name() ); - } - else { - final javax.persistence.ForeignKey jpaFk = inferredData.getProperty().getAnnotation( javax.persistence.ForeignKey.class ); - if ( jpaFk != null ) { - if ( jpaFk.value() == ConstraintMode.NO_CONSTRAINT ) { - value.setForeignKeyName( "none" ); - } - else { - value.setForeignKeyName( StringHelper.nullIfEmpty( jpaFk.name() ) ); - value.setForeignKeyDefinition( StringHelper.nullIfEmpty( jpaFk.foreignKeyDefinition() ) ); - } - } - } } /**