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:
parent
92f41e41f6
commit
d6bc53235a
|
@ -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 )
|
||||
bindForeignKeyNameAndDefinition(
|
||||
value,
|
||||
property,
|
||||
propertyHolder.getOverriddenForeignKey( StringHelper.qualify( propertyHolder.getPath(), propertyName ) ),
|
||||
joinColumn,
|
||||
joinColumns
|
||||
);
|
||||
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() ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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<>();
|
||||
|
|
|
@ -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() ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue