|
31 | 31 | import org.hibernate.annotations.DynamicUpdate;
|
32 | 32 | import org.hibernate.annotations.Filter;
|
33 | 33 | import org.hibernate.annotations.Filters;
|
34 |
| -import org.hibernate.annotations.ForeignKey; |
35 | 34 | import org.hibernate.annotations.HQLSelect;
|
36 | 35 | import org.hibernate.annotations.Immutable;
|
37 | 36 | import org.hibernate.annotations.Loader;
|
|
117 | 116 | import jakarta.persistence.DiscriminatorColumn;
|
118 | 117 | import jakarta.persistence.DiscriminatorValue;
|
119 | 118 | import jakarta.persistence.Entity;
|
| 119 | +import jakarta.persistence.ForeignKey; |
120 | 120 | import jakarta.persistence.GeneratedValue;
|
121 | 121 | import jakarta.persistence.IdClass;
|
122 | 122 | import jakarta.persistence.Inheritance;
|
|
139 | 139 | import static org.hibernate.boot.model.internal.BinderHelper.getOverridableAnnotation;
|
140 | 140 | import static org.hibernate.boot.model.internal.BinderHelper.hasToOneAnnotation;
|
141 | 141 | import static org.hibernate.boot.model.internal.BinderHelper.isDefault;
|
| 142 | +import static org.hibernate.boot.model.internal.BinderHelper.noConstraint; |
142 | 143 | import static org.hibernate.boot.model.internal.BinderHelper.toAliasEntityMap;
|
143 | 144 | import static org.hibernate.boot.model.internal.BinderHelper.toAliasTableMap;
|
144 | 145 | import static org.hibernate.boot.model.internal.EmbeddableBinder.fillEmbeddable;
|
@@ -843,34 +844,38 @@ private static void checkNoJoinColumns(XClass clazzToProcess) {
|
843 | 844 | }
|
844 | 845 |
|
845 | 846 | 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(); |
849 | 853 | }
|
850 | 854 | 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() ); |
869 | 859 | }
|
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() ) ); |
874 | 879 | }
|
875 | 880 | }
|
876 | 881 | }
|
|
0 commit comments