HHH-12320 HHH-12975 - Make sure foreign key name/mode is set prior to building the constraint.

(cherry picked from commit 41e49725ae)
This commit is contained in:
Chris Cranford 2018-10-10 10:42:56 -04:00 committed by gbadner
parent 92f41e41f6
commit d6bc53235a
2 changed files with 54 additions and 50 deletions

View File

@ -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<String, IdentifierGeneratorDefinition> buildGenerators(XAnnotatedElement annElt, MetadataBuildingContext context) {
InFlightMetadataCollector metadataCollector = context.getMetadataCollector();
HashMap<String, IdentifierGeneratorDefinition> generators = new HashMap<>();

View File

@ -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() ) );
}
}
}
}
/**