HHH-12320 HHH-12975 - Make sure foreign key name/mode is set prior to building the constraint.
This commit is contained in:
parent
9022085f5c
commit
41e49725ae
|
@ -3056,37 +3056,13 @@ public final class AnnotationBinder {
|
||||||
final String propertyName = inferredData.getPropertyName();
|
final String propertyName = inferredData.getPropertyName();
|
||||||
value.setTypeUsingReflection( propertyHolder.getClassName(), propertyName );
|
value.setTypeUsingReflection( propertyHolder.getClassName(), propertyName );
|
||||||
|
|
||||||
if ( ( joinColumn != null && joinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT )
|
bindForeignKeyNameAndDefinition(
|
||||||
|| ( joinColumns != null && joinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT ) ) {
|
value,
|
||||||
// not ideal...
|
property,
|
||||||
value.setForeignKeyName( "none" );
|
propertyHolder.getOverriddenForeignKey( StringHelper.qualify( propertyHolder.getPath(), propertyName ) ),
|
||||||
}
|
joinColumn,
|
||||||
else {
|
joinColumns
|
||||||
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() ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String path = propertyHolder.getPath() + "." + propertyName;
|
String path = propertyHolder.getPath() + "." + propertyName;
|
||||||
FkSecondPass secondPass = new ToOneFkSecondPass(
|
FkSecondPass secondPass = new ToOneFkSecondPass(
|
||||||
|
@ -3417,6 +3393,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) {
|
private static HashMap<String, IdentifierGeneratorDefinition> buildGenerators(XAnnotatedElement annElt, MetadataBuildingContext context) {
|
||||||
InFlightMetadataCollector metadataCollector = context.getMetadataCollector();
|
InFlightMetadataCollector metadataCollector = context.getMetadataCollector();
|
||||||
HashMap<String, IdentifierGeneratorDefinition> generators = new HashMap<>();
|
HashMap<String, IdentifierGeneratorDefinition> generators = new HashMap<>();
|
||||||
|
|
|
@ -8,11 +8,12 @@ package org.hibernate.cfg;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.persistence.ConstraintMode;
|
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.JoinColumns;
|
||||||
|
|
||||||
import org.hibernate.AnnotationException;
|
import org.hibernate.AnnotationException;
|
||||||
import org.hibernate.MappingException;
|
import org.hibernate.MappingException;
|
||||||
import org.hibernate.annotations.ForeignKey;
|
|
||||||
import org.hibernate.annotations.LazyGroup;
|
import org.hibernate.annotations.LazyGroup;
|
||||||
import org.hibernate.annotations.common.reflection.XClass;
|
import org.hibernate.annotations.common.reflection.XClass;
|
||||||
import org.hibernate.boot.spi.MetadataBuildingContext;
|
import org.hibernate.boot.spi.MetadataBuildingContext;
|
||||||
|
@ -104,6 +105,15 @@ public class OneToOneSecondPass implements SecondPass {
|
||||||
: ForeignKeyDirection.TO_PARENT
|
: 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();
|
PropertyBinder binder = new PropertyBinder();
|
||||||
binder.setName( propertyName );
|
binder.setName( propertyName );
|
||||||
binder.setValue( value );
|
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