Skip to content

Commit 5f8831e

Browse files
gavinkingsebersole
authored andcommitted
HHH-18185 finally remove @foreignkey (yay!)
Signed-off-by: Gavin King <gavin@hibernate.org>
1 parent 33f260a commit 5f8831e

File tree

23 files changed

+185
-316
lines changed

23 files changed

+185
-316
lines changed

documentation/src/main/asciidoc/introduction/Mapping.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ The `@SecondaryTable` annotation is even more interesting:
248248
| `uniqueConstraints` | One or more `@UniqueConstraint` annotations declaring multi-column unique constraints
249249
| `indexes` | One or more `@Index` annotations each declaring an index
250250
| `pkJoinColumns` | One or more `@PrimaryKeyJoinColumn` annotations, specifying <<primary-key-column-mappings,primary key column mappings>>
251-
| `foreignKey` | An `@ForeignKey` annotation specifying the name of the `FOREIGN KEY` constraint on the ``@PrimaryKeyJoinColumn``s
251+
| `foreignKey` | A `@ForeignKey` annotation specifying the name of the `FOREIGN KEY` constraint on the ``@PrimaryKeyJoinColumn``s
252252
|===
253253

254254
[TIP]
@@ -322,8 +322,8 @@ Here, there should be a `UNIQUE` constraint on _both_ columns of the association
322322
| `indexes` | One or more `@Index` annotations each declaring an index
323323
| `joinColumns` | One or more `@JoinColumn` annotations, specifying <<join-column-mappings,foreign key column mappings>> to the table of the owning side
324324
| `inverseJoinColumns` | One or more `@JoinColumn` annotations, specifying <<join-column-mappings,foreign key column mappings>> to the table of the unowned side
325-
| `foreignKey` | An `@ForeignKey` annotation specifying the name of the `FOREIGN KEY` constraint on the ``joinColumns``s
326-
| `inverseForeignKey` | An `@ForeignKey` annotation specifying the name of the `FOREIGN KEY` constraint on the ``inverseJoinColumns``s
325+
| `foreignKey` | A `@ForeignKey` annotation specifying the name of the `FOREIGN KEY` constraint on the ``joinColumns``s
326+
| `inverseForeignKey` | A `@ForeignKey` annotation specifying the name of the `FOREIGN KEY` constraint on the ``inverseJoinColumns``s
327327
|===
328328

329329
To better understand these annotations, we must first discuss column mappings in general.

hibernate-core/src/main/java/org/hibernate/annotations/ForeignKey.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

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

Lines changed: 81 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,82 +2107,76 @@ private DependantValue buildCollectionKey(AnnotatedJoinColumns joinColumns, OnDe
21072107
collection.setKey( key );
21082108

21092109
if ( property != null ) {
2110-
final org.hibernate.annotations.ForeignKey fk = property.getDirectAnnotationUsage( org.hibernate.annotations.ForeignKey.class );
2111-
if ( fk != null && !fk.name().isEmpty() ) {
2112-
key.setForeignKeyName( fk.name() );
2110+
final CollectionTable collectionTableAnn = property.getDirectAnnotationUsage( CollectionTable.class );
2111+
if ( collectionTableAnn != null ) {
2112+
final ForeignKey foreignKey = collectionTableAnn.foreignKey();
2113+
final ConstraintMode constraintMode = foreignKey.value();
2114+
if ( constraintMode == NO_CONSTRAINT
2115+
|| constraintMode == PROVIDER_DEFAULT && noConstraintByDefault ) {
2116+
key.disableForeignKey();
2117+
}
2118+
else {
2119+
key.setForeignKeyName( nullIfEmpty( foreignKey.name() ) );
2120+
key.setForeignKeyDefinition( nullIfEmpty( foreignKey.foreignKeyDefinition() ) );
2121+
if ( key.getForeignKeyName() == null
2122+
&& key.getForeignKeyDefinition() == null
2123+
&& collectionTableAnn.joinColumns().length == 1 ) {
2124+
//noinspection unchecked
2125+
final JoinColumn joinColumn = collectionTableAnn.joinColumns()[0];
2126+
final ForeignKey nestedForeignKey = joinColumn.foreignKey();
2127+
key.setForeignKeyName( nullIfEmpty( nestedForeignKey.name() ) );
2128+
key.setForeignKeyDefinition( nullIfEmpty( nestedForeignKey.foreignKeyDefinition() ) );
2129+
}
2130+
}
21132131
}
21142132
else {
2115-
final CollectionTable collectionTableAnn = property.getDirectAnnotationUsage( CollectionTable.class );
2116-
if ( collectionTableAnn != null ) {
2117-
final ForeignKey foreignKey = collectionTableAnn.foreignKey();
2118-
final ConstraintMode constraintMode = foreignKey.value();
2119-
if ( constraintMode == NO_CONSTRAINT
2120-
|| constraintMode == PROVIDER_DEFAULT && noConstraintByDefault ) {
2133+
final JoinTable joinTableAnn = property.getDirectAnnotationUsage( JoinTable.class );
2134+
if ( joinTableAnn != null ) {
2135+
final ForeignKey foreignKey = joinTableAnn.foreignKey();
2136+
String foreignKeyName = foreignKey.name();
2137+
String foreignKeyDefinition = foreignKey.foreignKeyDefinition();
2138+
ConstraintMode foreignKeyValue = foreignKey.value();
2139+
final JoinColumn[] joinColumnAnnotations = joinTableAnn.joinColumns();
2140+
if ( !ArrayHelper.isEmpty( joinColumnAnnotations ) ) {
2141+
final JoinColumn joinColumnAnn = joinColumnAnnotations[0];
2142+
final ForeignKey joinColumnForeignKey = joinColumnAnn.foreignKey();
2143+
if ( foreignKeyName.isEmpty() ) {
2144+
foreignKeyName = joinColumnForeignKey.name();
2145+
foreignKeyDefinition = joinColumnForeignKey.foreignKeyDefinition();
2146+
}
2147+
if ( foreignKeyValue != NO_CONSTRAINT ) {
2148+
foreignKeyValue = joinColumnForeignKey.value();
2149+
}
2150+
}
2151+
if ( foreignKeyValue == NO_CONSTRAINT
2152+
|| foreignKeyValue == PROVIDER_DEFAULT && noConstraintByDefault ) {
21212153
key.disableForeignKey();
21222154
}
21232155
else {
2124-
key.setForeignKeyName( nullIfEmpty( foreignKey.name() ) );
2125-
key.setForeignKeyDefinition( nullIfEmpty( foreignKey.foreignKeyDefinition() ) );
2126-
if ( key.getForeignKeyName() == null
2127-
&& key.getForeignKeyDefinition() == null
2128-
&& collectionTableAnn.joinColumns().length == 1 ) {
2129-
//noinspection unchecked
2130-
final JoinColumn joinColumn = collectionTableAnn.joinColumns()[0];
2131-
final ForeignKey nestedForeignKey = joinColumn.foreignKey();
2132-
key.setForeignKeyName( nullIfEmpty( nestedForeignKey.name() ) );
2133-
key.setForeignKeyDefinition( nullIfEmpty( nestedForeignKey.foreignKeyDefinition() ) );
2134-
}
2156+
key.setForeignKeyName( nullIfEmpty( foreignKeyName ) );
2157+
key.setForeignKeyDefinition( nullIfEmpty( foreignKeyDefinition ) );
21352158
}
21362159
}
21372160
else {
2138-
final JoinTable joinTableAnn = property.getDirectAnnotationUsage( JoinTable.class );
2139-
if ( joinTableAnn != null ) {
2140-
final ForeignKey foreignKey = joinTableAnn.foreignKey();
2141-
String foreignKeyName = foreignKey.name();
2142-
String foreignKeyDefinition = foreignKey.foreignKeyDefinition();
2143-
ConstraintMode foreignKeyValue = foreignKey.value();
2144-
final JoinColumn[] joinColumnAnnotations = joinTableAnn.joinColumns();
2145-
if ( !ArrayHelper.isEmpty( joinColumnAnnotations ) ) {
2146-
final JoinColumn joinColumnAnn = joinColumnAnnotations[0];
2147-
final ForeignKey joinColumnForeignKey = joinColumnAnn.foreignKey();
2148-
if ( foreignKeyName.isEmpty() ) {
2149-
foreignKeyName = joinColumnForeignKey.name();
2150-
foreignKeyDefinition = joinColumnForeignKey.foreignKeyDefinition();
2151-
}
2152-
if ( foreignKeyValue != NO_CONSTRAINT ) {
2153-
foreignKeyValue = joinColumnForeignKey.value();
2154-
}
2155-
}
2156-
if ( foreignKeyValue == NO_CONSTRAINT
2157-
|| foreignKeyValue == PROVIDER_DEFAULT && noConstraintByDefault ) {
2158-
key.disableForeignKey();
2159-
}
2160-
else {
2161-
key.setForeignKeyName( nullIfEmpty( foreignKeyName ) );
2162-
key.setForeignKeyDefinition( nullIfEmpty( foreignKeyDefinition ) );
2163-
}
2161+
final String propertyPath = qualify( propertyHolder.getPath(), property.getName() );
2162+
final ForeignKey foreignKey = propertyHolder.getOverriddenForeignKey( propertyPath );
2163+
if ( foreignKey != null ) {
2164+
handleForeignKeyConstraint( noConstraintByDefault, key, foreignKey );
21642165
}
21652166
else {
2166-
final String propertyPath = qualify( propertyHolder.getPath(), property.getName() );
2167-
final ForeignKey foreignKey = propertyHolder.getOverriddenForeignKey( propertyPath );
2168-
if ( foreignKey != null ) {
2169-
handleForeignKeyConstraint( noConstraintByDefault, key, foreignKey );
2167+
final OneToMany oneToManyAnn = property.getDirectAnnotationUsage( OneToMany.class );
2168+
final OnDelete onDeleteAnn = property.getDirectAnnotationUsage( OnDelete.class );
2169+
if ( oneToManyAnn != null
2170+
&& !oneToManyAnn.mappedBy().isEmpty()
2171+
&& ( onDeleteAnn == null || onDeleteAnn.action() != OnDeleteAction.CASCADE ) ) {
2172+
// foreign key should be up to @ManyToOne side
2173+
// @OnDelete generate "on delete cascade" foreign key
2174+
key.disableForeignKey();
21702175
}
21712176
else {
2172-
final OneToMany oneToManyAnn = property.getDirectAnnotationUsage( OneToMany.class );
2173-
final OnDelete onDeleteAnn = property.getDirectAnnotationUsage( OnDelete.class );
2174-
if ( oneToManyAnn != null
2175-
&& !oneToManyAnn.mappedBy().isEmpty()
2176-
&& ( onDeleteAnn == null || onDeleteAnn.action() != OnDeleteAction.CASCADE ) ) {
2177-
// foreign key should be up to @ManyToOne side
2178-
// @OnDelete generate "on delete cascade" foreign key
2179-
key.disableForeignKey();
2180-
}
2181-
else {
2182-
final JoinColumn joinColumnAnn = property.getDirectAnnotationUsage( JoinColumn.class );
2183-
if ( joinColumnAnn != null ) {
2184-
handleForeignKeyConstraint( noConstraintByDefault, key, joinColumnAnn.foreignKey() );
2185-
}
2177+
final JoinColumn joinColumnAnn = property.getDirectAnnotationUsage( JoinColumn.class );
2178+
if ( joinColumnAnn != null ) {
2179+
handleForeignKeyConstraint( noConstraintByDefault, key, joinColumnAnn.foreignKey() );
21862180
}
21872181
}
21882182
}
@@ -2466,38 +2460,32 @@ private ManyToOne handleCollectionOfEntities(
24662460
collection.setManyToManyOrdering( buildOrderByClauseFromHql( hqlOrderBy, collectionEntity ) );
24672461
}
24682462

2469-
final org.hibernate.annotations.ForeignKey fk = property.getDirectAnnotationUsage( org.hibernate.annotations.ForeignKey.class );
2470-
if ( fk != null && !fk.name().isEmpty() ) {
2471-
element.setForeignKeyName( fk.name() );
2472-
}
2473-
else {
2474-
final JoinTable joinTableAnn = property.getDirectAnnotationUsage( JoinTable.class );
2475-
if ( joinTableAnn != null ) {
2476-
final ForeignKey inverseForeignKey = joinTableAnn.inverseForeignKey();
2477-
String foreignKeyName = inverseForeignKey.name();
2478-
String foreignKeyDefinition = inverseForeignKey.foreignKeyDefinition();
2479-
2480-
final JoinColumn[] inverseJoinColumns = joinTableAnn.inverseJoinColumns();
2481-
if ( !ArrayHelper.isEmpty( inverseJoinColumns ) ) {
2482-
final JoinColumn joinColumnAnn = inverseJoinColumns[0];
2483-
if ( foreignKeyName.isEmpty() ) {
2484-
final ForeignKey inverseJoinColumnForeignKey = joinColumnAnn.foreignKey();
2485-
foreignKeyName = inverseJoinColumnForeignKey.name();
2486-
foreignKeyDefinition = inverseJoinColumnForeignKey.foreignKeyDefinition();
2487-
}
2488-
}
2463+
final JoinTable joinTableAnn = property.getDirectAnnotationUsage( JoinTable.class );
2464+
if ( joinTableAnn != null ) {
2465+
final ForeignKey inverseForeignKey = joinTableAnn.inverseForeignKey();
2466+
String foreignKeyName = inverseForeignKey.name();
2467+
String foreignKeyDefinition = inverseForeignKey.foreignKeyDefinition();
24892468

2490-
final ConstraintMode constraintMode = inverseForeignKey.value();
2491-
if ( constraintMode == NO_CONSTRAINT
2492-
|| constraintMode == PROVIDER_DEFAULT
2493-
&& buildingContext.getBuildingOptions().isNoConstraintByDefault() ) {
2494-
element.disableForeignKey();
2495-
}
2496-
else {
2497-
element.setForeignKeyName( nullIfEmpty( foreignKeyName ) );
2498-
element.setForeignKeyDefinition( nullIfEmpty( foreignKeyDefinition ) );
2469+
final JoinColumn[] inverseJoinColumns = joinTableAnn.inverseJoinColumns();
2470+
if ( !ArrayHelper.isEmpty( inverseJoinColumns ) ) {
2471+
final JoinColumn joinColumnAnn = inverseJoinColumns[0];
2472+
if ( foreignKeyName.isEmpty() ) {
2473+
final ForeignKey inverseJoinColumnForeignKey = joinColumnAnn.foreignKey();
2474+
foreignKeyName = inverseJoinColumnForeignKey.name();
2475+
foreignKeyDefinition = inverseJoinColumnForeignKey.foreignKeyDefinition();
24992476
}
25002477
}
2478+
2479+
final ConstraintMode constraintMode = inverseForeignKey.value();
2480+
if ( constraintMode == NO_CONSTRAINT
2481+
|| constraintMode == PROVIDER_DEFAULT
2482+
&& buildingContext.getBuildingOptions().isNoConstraintByDefault() ) {
2483+
element.disableForeignKey();
2484+
}
2485+
else {
2486+
element.setForeignKeyName( nullIfEmpty( foreignKeyName ) );
2487+
element.setForeignKeyDefinition( nullIfEmpty( foreignKeyDefinition ) );
2488+
}
25012489
}
25022490
return element;
25032491
}

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

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -898,32 +898,26 @@ private void handleForeignKeys(ClassDetails clazzToProcess, MetadataBuildingCont
898898
key.disableForeignKey();
899899
}
900900
else {
901-
final org.hibernate.annotations.ForeignKey fk = clazzToProcess.getDirectAnnotationUsage( org.hibernate.annotations.ForeignKey.class);
902-
if ( fk != null && isNotEmpty( fk.name() ) ) {
903-
key.setForeignKeyName( fk.name() );
901+
final ForeignKey foreignKey = clazzToProcess.getDirectAnnotationUsage( ForeignKey.class );
902+
if ( noConstraint( foreignKey, noConstraintByDefault ) ) {
903+
key.disableForeignKey();
904904
}
905-
else {
906-
final ForeignKey foreignKey = clazzToProcess.getDirectAnnotationUsage( ForeignKey.class );
907-
if ( noConstraint( foreignKey, noConstraintByDefault ) ) {
908-
key.disableForeignKey();
909-
}
910-
else if ( foreignKey != null ) {
911-
key.setForeignKeyName( nullIfEmpty( foreignKey.name() ) );
912-
key.setForeignKeyDefinition( nullIfEmpty( foreignKey.foreignKeyDefinition() ) );
913-
}
914-
else if ( noConstraintByDefault ) {
915-
key.disableForeignKey();
916-
}
917-
else if ( pkJoinColumns != null ) {
918-
final ForeignKey nestedFk = pkJoinColumns.foreignKey();
919-
key.setForeignKeyName( nullIfEmpty( nestedFk.name() ) );
920-
key.setForeignKeyDefinition( nullIfEmpty( nestedFk.foreignKeyDefinition() ) );
921-
}
922-
else if ( pkJoinColumn != null ) {
923-
final ForeignKey nestedFk = pkJoinColumn.foreignKey();
924-
key.setForeignKeyName( nullIfEmpty( nestedFk.name() ) );
925-
key.setForeignKeyDefinition( nullIfEmpty( nestedFk.foreignKeyDefinition() ) );
926-
}
905+
else if ( foreignKey != null ) {
906+
key.setForeignKeyName( nullIfEmpty( foreignKey.name() ) );
907+
key.setForeignKeyDefinition( nullIfEmpty( foreignKey.foreignKeyDefinition() ) );
908+
}
909+
else if ( noConstraintByDefault ) {
910+
key.disableForeignKey();
911+
}
912+
else if ( pkJoinColumns != null ) {
913+
final ForeignKey nestedFk = pkJoinColumns.foreignKey();
914+
key.setForeignKeyName( nullIfEmpty( nestedFk.name() ) );
915+
key.setForeignKeyDefinition( nullIfEmpty( nestedFk.foreignKeyDefinition() ) );
916+
}
917+
else if ( pkJoinColumn != null ) {
918+
final ForeignKey nestedFk = pkJoinColumn.foreignKey();
919+
key.setForeignKeyName( nullIfEmpty( nestedFk.name() ) );
920+
key.setForeignKeyDefinition( nullIfEmpty( nestedFk.foreignKeyDefinition() ) );
927921
}
928922
}
929923
}

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

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -622,32 +622,25 @@ public static void bindForeignKeyNameAndDefinition(
622622
value.disableForeignKey();
623623
}
624624
else {
625-
final org.hibernate.annotations.ForeignKey fk =
626-
property.getDirectAnnotationUsage( org.hibernate.annotations.ForeignKey.class );
627-
if ( fk != null && isNotEmpty( fk.name() ) ) {
628-
value.setForeignKeyName( fk.name() );
625+
if ( noConstraint( foreignKey, noConstraintByDefault ) ) {
626+
value.disableForeignKey();
629627
}
630-
else {
631-
if ( noConstraint( foreignKey, noConstraintByDefault ) ) {
632-
value.disableForeignKey();
633-
}
634-
else if ( foreignKey != null ) {
635-
value.setForeignKeyName( nullIfEmpty( foreignKey.name() ) );
636-
value.setForeignKeyDefinition( nullIfEmpty( foreignKey.foreignKeyDefinition() ) );
637-
}
638-
else if ( noConstraintByDefault ) {
639-
value.disableForeignKey();
640-
}
641-
else if ( joinColumns != null ) {
642-
final ForeignKey joinColumnsForeignKey = joinColumns.foreignKey();
643-
value.setForeignKeyName( nullIfEmpty( joinColumnsForeignKey.name() ) );
644-
value.setForeignKeyDefinition( nullIfEmpty( joinColumnsForeignKey.foreignKeyDefinition() ) );
645-
}
646-
else if ( joinColumn != null ) {
647-
final ForeignKey joinColumnForeignKey = joinColumn.foreignKey();
648-
value.setForeignKeyName( nullIfEmpty( joinColumnForeignKey.name() ) );
649-
value.setForeignKeyDefinition( nullIfEmpty( joinColumnForeignKey.foreignKeyDefinition() ) );
650-
}
628+
else if ( foreignKey != null ) {
629+
value.setForeignKeyName( nullIfEmpty( foreignKey.name() ) );
630+
value.setForeignKeyDefinition( nullIfEmpty( foreignKey.foreignKeyDefinition() ) );
631+
}
632+
else if ( noConstraintByDefault ) {
633+
value.disableForeignKey();
634+
}
635+
else if ( joinColumns != null ) {
636+
final ForeignKey joinColumnsForeignKey = joinColumns.foreignKey();
637+
value.setForeignKeyName( nullIfEmpty( joinColumnsForeignKey.name() ) );
638+
value.setForeignKeyDefinition( nullIfEmpty( joinColumnsForeignKey.foreignKeyDefinition() ) );
639+
}
640+
else if ( joinColumn != null ) {
641+
final ForeignKey joinColumnForeignKey = joinColumn.foreignKey();
642+
value.setForeignKeyName( nullIfEmpty( joinColumnForeignKey.name() ) );
643+
value.setForeignKeyDefinition( nullIfEmpty( joinColumnForeignKey.foreignKeyDefinition() ) );
651644
}
652645
}
653646
}

0 commit comments

Comments
 (0)