Skip to content

Commit bdb2906

Browse files
mbelladebeikov
authored andcommitted
HHH-17550 Respect default no-constraint setting
1 parent 641afbc commit bdb2906

File tree

3 files changed

+54
-40
lines changed

3 files changed

+54
-40
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/BinderHelper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@
6262
import org.hibernate.mapping.Value;
6363
import org.hibernate.type.descriptor.java.JavaType;
6464

65+
import jakarta.persistence.ConstraintMode;
6566
import jakarta.persistence.Embeddable;
6667
import jakarta.persistence.EmbeddedId;
6768
import jakarta.persistence.FetchType;
69+
import jakarta.persistence.ForeignKey;
6870
import jakarta.persistence.ManyToOne;
6971
import jakarta.persistence.OneToOne;
7072

73+
import static jakarta.persistence.ConstraintMode.NO_CONSTRAINT;
74+
import static jakarta.persistence.ConstraintMode.PROVIDER_DEFAULT;
7175
import static org.hibernate.boot.model.internal.AnnotatedColumn.buildColumnOrFormulaFromAnnotation;
7276
import static org.hibernate.boot.model.internal.HCANNHelper.findAnnotation;
7377
import static org.hibernate.internal.util.StringHelper.isEmpty;
@@ -1124,6 +1128,17 @@ private static boolean checkReferencedClass(PersistentClass ownerClass, Persiste
11241128
return false;
11251129
}
11261130

1131+
public static boolean noConstraint(ForeignKey foreignKey, boolean noConstraintByDefault) {
1132+
if ( foreignKey == null ) {
1133+
return false;
1134+
}
1135+
else {
1136+
final ConstraintMode mode = foreignKey.value();
1137+
return mode == NO_CONSTRAINT
1138+
|| mode == PROVIDER_DEFAULT && noConstraintByDefault;
1139+
}
1140+
}
1141+
11271142
/**
11281143
* Extract an annotation from the package-info for the package the given class is defined in
11291144
*

hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.hibernate.annotations.DynamicUpdate;
3232
import org.hibernate.annotations.Filter;
3333
import org.hibernate.annotations.Filters;
34-
import org.hibernate.annotations.ForeignKey;
3534
import org.hibernate.annotations.HQLSelect;
3635
import org.hibernate.annotations.Immutable;
3736
import org.hibernate.annotations.Loader;
@@ -117,6 +116,7 @@
117116
import jakarta.persistence.DiscriminatorColumn;
118117
import jakarta.persistence.DiscriminatorValue;
119118
import jakarta.persistence.Entity;
119+
import jakarta.persistence.ForeignKey;
120120
import jakarta.persistence.GeneratedValue;
121121
import jakarta.persistence.IdClass;
122122
import jakarta.persistence.Inheritance;
@@ -139,6 +139,7 @@
139139
import static org.hibernate.boot.model.internal.BinderHelper.getOverridableAnnotation;
140140
import static org.hibernate.boot.model.internal.BinderHelper.hasToOneAnnotation;
141141
import static org.hibernate.boot.model.internal.BinderHelper.isDefault;
142+
import static org.hibernate.boot.model.internal.BinderHelper.noConstraint;
142143
import static org.hibernate.boot.model.internal.BinderHelper.toAliasEntityMap;
143144
import static org.hibernate.boot.model.internal.BinderHelper.toAliasTableMap;
144145
import static org.hibernate.boot.model.internal.EmbeddableBinder.fillEmbeddable;
@@ -843,34 +844,38 @@ private static void checkNoJoinColumns(XClass clazzToProcess) {
843844
}
844845

845846
private static void handleForeignKeys(XClass clazzToProcess, MetadataBuildingContext context, DependantValue key) {
846-
final ForeignKey foreignKey = clazzToProcess.getAnnotation( ForeignKey.class );
847-
if ( foreignKey != null && !foreignKey.name().isEmpty() ) {
848-
key.setForeignKeyName( foreignKey.name() );
847+
final PrimaryKeyJoinColumn pkJoinColumn = clazzToProcess.getAnnotation( PrimaryKeyJoinColumn.class );
848+
final PrimaryKeyJoinColumns pkJoinColumns = clazzToProcess.getAnnotation( PrimaryKeyJoinColumns.class );
849+
final boolean noConstraintByDefault = context.getBuildingOptions().isNoConstraintByDefault();
850+
if ( pkJoinColumn != null && noConstraint( pkJoinColumn.foreignKey(), noConstraintByDefault )
851+
|| pkJoinColumns != null && noConstraint( pkJoinColumns.foreignKey(), noConstraintByDefault ) ) {
852+
key.disableForeignKey();
849853
}
850854
else {
851-
final PrimaryKeyJoinColumn pkJoinColumn = clazzToProcess.getAnnotation( PrimaryKeyJoinColumn.class );
852-
final PrimaryKeyJoinColumns pkJoinColumns = clazzToProcess.getAnnotation( PrimaryKeyJoinColumns.class );
853-
final boolean noConstraintByDefault = context.getBuildingOptions().isNoConstraintByDefault();
854-
if ( pkJoinColumns != null && ( pkJoinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT
855-
|| pkJoinColumns.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault ) ) {
856-
// don't apply a constraint based on ConstraintMode
857-
key.disableForeignKey();
858-
}
859-
else if ( pkJoinColumns != null && isNotEmpty( pkJoinColumns.foreignKey().name() ) ) {
860-
key.setForeignKeyName( pkJoinColumns.foreignKey().name() );
861-
if ( !pkJoinColumns.foreignKey().foreignKeyDefinition().isEmpty() ) {
862-
key.setForeignKeyDefinition( pkJoinColumns.foreignKey().foreignKeyDefinition() );
863-
}
864-
}
865-
else if ( pkJoinColumn != null && ( pkJoinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT
866-
|| pkJoinColumn.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault ) ) {
867-
// don't apply a constraint based on ConstraintMode
868-
key.disableForeignKey();
855+
final org.hibernate.annotations.ForeignKey fk =
856+
clazzToProcess.getAnnotation( org.hibernate.annotations.ForeignKey.class );
857+
if ( fk != null && isNotEmpty( fk.name() ) ) {
858+
key.setForeignKeyName( fk.name() );
869859
}
870-
else if ( pkJoinColumn != null && isNotEmpty( pkJoinColumn.foreignKey().name() ) ) {
871-
key.setForeignKeyName( pkJoinColumn.foreignKey().name() );
872-
if ( !pkJoinColumn.foreignKey().foreignKeyDefinition().isEmpty() ) {
873-
key.setForeignKeyDefinition( pkJoinColumn.foreignKey().foreignKeyDefinition() );
860+
else {
861+
final ForeignKey foreignKey = clazzToProcess.getAnnotation( ForeignKey.class );
862+
if ( noConstraint( foreignKey, noConstraintByDefault ) ) {
863+
key.disableForeignKey();
864+
}
865+
else if ( foreignKey != null ) {
866+
key.setForeignKeyName( nullIfEmpty( foreignKey.name() ) );
867+
key.setForeignKeyDefinition( nullIfEmpty( foreignKey.foreignKeyDefinition() ) );
868+
}
869+
else if ( noConstraintByDefault ) {
870+
key.disableForeignKey();
871+
}
872+
else if ( pkJoinColumns != null ) {
873+
key.setForeignKeyName( nullIfEmpty( pkJoinColumns.foreignKey().name() ) );
874+
key.setForeignKeyDefinition( nullIfEmpty( pkJoinColumns.foreignKey().foreignKeyDefinition() ) );
875+
}
876+
else if ( pkJoinColumn != null ) {
877+
key.setForeignKeyName( nullIfEmpty( pkJoinColumn.foreignKey().name() ) );
878+
key.setForeignKeyDefinition( nullIfEmpty( pkJoinColumn.foreignKey().foreignKeyDefinition() ) );
874879
}
875880
}
876881
}

hibernate-core/src/main/java/org/hibernate/boot/model/internal/ToOneBinder.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import static org.hibernate.boot.model.internal.BinderHelper.getFetchMode;
5858
import static org.hibernate.boot.model.internal.BinderHelper.getPath;
5959
import static org.hibernate.boot.model.internal.BinderHelper.isDefault;
60+
import static org.hibernate.boot.model.internal.BinderHelper.noConstraint;
6061
import static org.hibernate.internal.CoreLogging.messageLogger;
6162
import static org.hibernate.internal.util.StringHelper.isEmpty;
6263
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
@@ -610,8 +611,9 @@ public static void bindForeignKeyNameAndDefinition(
610611
else {
611612
final JoinColumn joinColumn = property.getAnnotation( JoinColumn.class );
612613
final JoinColumns joinColumns = property.getAnnotation( JoinColumns.class );
613-
if ( joinColumn!=null && noConstraint( joinColumn.foreignKey(), context )
614-
|| joinColumns!=null && noConstraint( joinColumns.foreignKey(), context ) ) {
614+
final boolean noConstraintByDefault = context.getBuildingOptions().isNoConstraintByDefault();
615+
if ( joinColumn != null && noConstraint( joinColumn.foreignKey(), noConstraintByDefault )
616+
|| joinColumns != null && noConstraint( joinColumns.foreignKey(), noConstraintByDefault ) ) {
615617
value.disableForeignKey();
616618
}
617619
else {
@@ -621,13 +623,16 @@ public static void bindForeignKeyNameAndDefinition(
621623
value.setForeignKeyName( fk.name() );
622624
}
623625
else {
624-
if ( noConstraint( foreignKey, context ) ) {
626+
if ( noConstraint( foreignKey, noConstraintByDefault ) ) {
625627
value.disableForeignKey();
626628
}
627629
else if ( foreignKey != null ) {
628630
value.setForeignKeyName( nullIfEmpty( foreignKey.name() ) );
629631
value.setForeignKeyDefinition( nullIfEmpty( foreignKey.foreignKeyDefinition() ) );
630632
}
633+
else if ( noConstraintByDefault ) {
634+
value.disableForeignKey();
635+
}
631636
else if ( joinColumns != null ) {
632637
value.setForeignKeyName( nullIfEmpty( joinColumns.foreignKey().name() ) );
633638
value.setForeignKeyDefinition( nullIfEmpty( joinColumns.foreignKey().foreignKeyDefinition() ) );
@@ -641,17 +646,6 @@ else if ( joinColumn != null ) {
641646
}
642647
}
643648

644-
private static boolean noConstraint(ForeignKey joinColumns, MetadataBuildingContext context) {
645-
if ( joinColumns == null ) {
646-
return false;
647-
}
648-
else {
649-
final ConstraintMode mode = joinColumns.value();
650-
return mode == NO_CONSTRAINT
651-
|| mode == PROVIDER_DEFAULT && context.getBuildingOptions().isNoConstraintByDefault();
652-
}
653-
}
654-
655649
public static String getReferenceEntityName(PropertyData propertyData, XClass targetEntity, MetadataBuildingContext context) {
656650
return isDefault( targetEntity, context )
657651
? propertyData.getClassOrElementName()

0 commit comments

Comments
 (0)