From 78de650efec58367a4196eb41c1d5f99b291f036 Mon Sep 17 00:00:00 2001 From: Matthias Kurz Date: Sun, 29 May 2016 22:17:48 +0200 Subject: [PATCH] HHH-10643 - Attribute 'foreignKeyDefinition' of @javax.persistence.ForeignKey ignored by schema exporter --- .../org/hibernate/cfg/AnnotationBinder.java | 1 + .../org/hibernate/cfg/OneToOneSecondPass.java | 1 + .../cfg/annotations/CollectionBinder.java | 8 ++ .../cfg/annotations/EntityBinder.java | 1 + .../java/org/hibernate/dialect/Dialect.java | 11 +++ .../hibernate/dialect/InformixDialect.java | 11 +++ .../org/hibernate/dialect/SAPDBDialect.java | 6 ++ .../hibernate/mapping/DenormalizedTable.java | 1 + .../org/hibernate/mapping/ForeignKey.java | 33 ++++++-- .../java/org/hibernate/mapping/ManyToOne.java | 1 + .../org/hibernate/mapping/SimpleValue.java | 11 ++- .../java/org/hibernate/mapping/Table.java | 6 +- .../internal/StandardForeignKeyExporter.java | 19 +++-- .../ForeignKeyMigrationTest.java | 2 +- .../AbstractForeignKeyDefinitionTest.java | 81 +++++++++++++++++++ .../ForeignKeyDefinitionManyToOneTest.java | 54 +++++++++++++ ...gnKeyDefinitionOneToManyJoinTableTest.java | 69 ++++++++++++++++ .../ForeignKeyDefinitionOneToOneTest.java | 54 +++++++++++++ ...oreignKeyDefinitionSecondaryTableTest.java | 56 +++++++++++++ 19 files changed, 408 insertions(+), 18 deletions(-) rename hibernate-core/src/test/java/org/hibernate/test/schemaupdate/{ => foreignkeys}/ForeignKeyMigrationTest.java (97%) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/AbstractForeignKeyDefinitionTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionManyToOneTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionOneToManyJoinTableTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionOneToOneTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionSecondaryTableTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 740cce421e..820db3c5b0 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -2925,6 +2925,7 @@ public final class AnnotationBinder { } else if ( joinColumn != null ) { value.setForeignKeyName( StringHelper.nullIfEmpty( joinColumn.foreignKey().name() ) ); + value.setForeignKeyDefinition( StringHelper.nullIfEmpty( joinColumn.foreignKey().foreignKeyDefinition() ) ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/OneToOneSecondPass.java b/hibernate-core/src/main/java/org/hibernate/cfg/OneToOneSecondPass.java index c5cdf59393..c3d64ae86c 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/OneToOneSecondPass.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/OneToOneSecondPass.java @@ -258,6 +258,7 @@ public class OneToOneSecondPass implements SecondPass { } else { value.setForeignKeyName( StringHelper.nullIfEmpty( jpaFk.name() ) ); + value.setForeignKeyDefinition( StringHelper.nullIfEmpty( jpaFk.foreignKeyDefinition() ) ); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java index 1a72915b36..960bc1bdb3 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java @@ -1132,17 +1132,20 @@ public abstract class CollectionBinder { } else { key.setForeignKeyName( StringHelper.nullIfEmpty( collectionTableAnn.foreignKey().name() ) ); + key.setForeignKeyDefinition( StringHelper.nullIfEmpty( collectionTableAnn.foreignKey().foreignKeyDefinition() ) ); } } else { final JoinTable joinTableAnn = property.getAnnotation( JoinTable.class ); if ( joinTableAnn != null ) { String foreignKeyName = joinTableAnn.foreignKey().name(); + String foreignKeyDefinition = joinTableAnn.foreignKey().foreignKeyDefinition(); ConstraintMode foreignKeyValue = joinTableAnn.foreignKey().value(); if ( joinTableAnn.joinColumns().length != 0 ) { final JoinColumn joinColumnAnn = joinTableAnn.joinColumns()[0]; if ( "".equals( foreignKeyName ) ) { foreignKeyName = joinColumnAnn.foreignKey().name(); + foreignKeyDefinition = joinColumnAnn.foreignKey().foreignKeyDefinition(); } if ( foreignKeyValue != ConstraintMode.NO_CONSTRAINT ) { foreignKeyValue = joinColumnAnn.foreignKey().value(); @@ -1153,6 +1156,7 @@ public abstract class CollectionBinder { } else { key.setForeignKeyName( StringHelper.nullIfEmpty( foreignKeyName ) ); + key.setForeignKeyDefinition( StringHelper.nullIfEmpty( foreignKeyDefinition ) ); } } else { @@ -1163,6 +1167,7 @@ public abstract class CollectionBinder { } else { key.setForeignKeyName( StringHelper.nullIfEmpty( joinColumnAnn.foreignKey().name() ) ); + key.setForeignKeyDefinition( StringHelper.nullIfEmpty( joinColumnAnn.foreignKey().foreignKeyDefinition() ) ); } } } @@ -1342,11 +1347,13 @@ public abstract class CollectionBinder { final JoinTable joinTableAnn = property.getAnnotation( JoinTable.class ); if ( joinTableAnn != null ) { String foreignKeyName = joinTableAnn.inverseForeignKey().name(); + String foreignKeyDefinition = joinTableAnn.inverseForeignKey().foreignKeyDefinition(); ConstraintMode foreignKeyValue = joinTableAnn.foreignKey().value(); if ( joinTableAnn.inverseJoinColumns().length != 0 ) { final JoinColumn joinColumnAnn = joinTableAnn.inverseJoinColumns()[0]; if ( "".equals( foreignKeyName ) ) { foreignKeyName = joinColumnAnn.foreignKey().name(); + foreignKeyDefinition = joinColumnAnn.foreignKey().foreignKeyDefinition(); } if ( foreignKeyValue != ConstraintMode.NO_CONSTRAINT ) { foreignKeyValue = joinColumnAnn.foreignKey().value(); @@ -1357,6 +1364,7 @@ public abstract class CollectionBinder { } else { element.setForeignKeyName( StringHelper.nullIfEmpty( foreignKeyName ) ); + element.setForeignKeyDefinition( StringHelper.nullIfEmpty( foreignKeyDefinition ) ); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java index 510af35f29..5e3a3cedf5 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java @@ -807,6 +807,7 @@ public class EntityBinder { } else { ( (SimpleValue) join.getKey() ).setForeignKeyName( StringHelper.nullIfEmpty( jpaSecondaryTable.foreignKey().name() ) ); + ( (SimpleValue) join.getKey() ).setForeignKeyDefinition( StringHelper.nullIfEmpty( jpaSecondaryTable.foreignKey().foreignKeyDefinition() ) ); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java index c65b20ee85..3d57c4a03b 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java @@ -2073,6 +2073,17 @@ public abstract class Dialect implements ConversionContext { return res.toString(); } + public String getAddForeignKeyConstraintString( + String constraintName, + String foreignKeyDefinition) { + return new StringBuilder( 30 ) + .append( " add constraint " ) + .append( quote( constraintName ) ) + .append( " " ) + .append( foreignKeyDefinition ) + .toString(); + } + /** * The syntax used to add a primary key constraint to a table. * diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/InformixDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/InformixDialect.java index 77a2543a6c..fd943096a7 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/InformixDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/InformixDialect.java @@ -111,6 +111,17 @@ public class InformixDialect extends Dialect { return result.toString(); } + public String getAddForeignKeyConstraintString( + String constraintName, + String foreignKeyDefinition) { + return new StringBuilder( 30 ) + .append( " add constraint " ) + .append( foreignKeyDefinition ) + .append( " constraint " ) + .append( constraintName ) + .toString(); + } + /** * Informix constraint name must be at the end. *

diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/SAPDBDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/SAPDBDialect.java index 998887671a..8ec9fa18b0 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/SAPDBDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/SAPDBDialect.java @@ -161,6 +161,12 @@ public class SAPDBDialect extends Dialect { return res.toString(); } + public String getAddForeignKeyConstraintString( + String constraintName, + String foreignKeyDefinition) { + return foreignKeyDefinition; + } + @Override public String getAddPrimaryKeyConstraintString(String constraintName) { return " primary key "; diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/DenormalizedTable.java b/hibernate-core/src/main/java/org/hibernate/mapping/DenormalizedTable.java index 7668f49a33..74362c4691 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/DenormalizedTable.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/DenormalizedTable.java @@ -64,6 +64,7 @@ public class DenormalizedTable extends Table { ), fk.getColumns(), fk.getReferencedEntityName(), + fk.getKeyDefinition(), fk.getReferencedColumns() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/ForeignKey.java b/hibernate-core/src/main/java/org/hibernate/mapping/ForeignKey.java index 09c3bc9851..f0da8b0fe1 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/ForeignKey.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/ForeignKey.java @@ -22,6 +22,7 @@ import org.hibernate.internal.util.StringHelper; public class ForeignKey extends Constraint { private Table referencedTable; private String referencedEntityName; + private String keyDefinition; private boolean cascadeDeleteEnabled; private List referencedColumns = new ArrayList(); private boolean creationEnabled = true; @@ -77,13 +78,23 @@ public class ForeignKey extends Constraint { i++; } - final String result = dialect.getAddForeignKeyConstraintString( - constraintName, - columnNames, - referencedTable.getQualifiedName( dialect, defaultCatalog, defaultSchema ), - referencedColumnNames, - isReferenceToPrimaryKey() - ); + final String result = keyDefinition != null ? + dialect.getAddForeignKeyConstraintString( + constraintName, + keyDefinition + ) : + dialect.getAddForeignKeyConstraintString( + constraintName, + columnNames, + referencedTable.getQualifiedName( + dialect, + defaultCatalog, + defaultSchema + ), + referencedColumnNames, + isReferenceToPrimaryKey() + ); + return cascadeDeleteEnabled && dialect.supportsCascadeDelete() ? result + " on delete cascade" : result; @@ -153,6 +164,14 @@ public class ForeignKey extends Constraint { this.referencedEntityName = referencedEntityName; } + public String getKeyDefinition() { + return keyDefinition; + } + + public void setKeyDefinition(String keyDefinition) { + this.keyDefinition = keyDefinition; + } + public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) { final StringBuilder buf = new StringBuilder( "alter table " ); buf.append( getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema ) ); diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/ManyToOne.java b/hibernate-core/src/main/java/org/hibernate/mapping/ManyToOne.java index 52cc461bd6..8c41ae4a01 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/ManyToOne.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/ManyToOne.java @@ -74,6 +74,7 @@ public class ManyToOne extends ToOne { getForeignKeyName(), getConstraintColumns(), ( (EntityType) getType() ).getAssociatedEntityName(), + getForeignKeyDefinition(), refColumns ); fk.setCascadeDeleteEnabled(isCascadeDeleteEnabled() ); diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java index 6041da0de2..6da536e8b0 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java @@ -73,6 +73,7 @@ public class SimpleValue implements KeyValue { private String nullValue; private Table table; private String foreignKeyName; + private String foreignKeyDefinition; private boolean alternateUniqueKey; private boolean cascadeDeleteEnabled; @@ -193,7 +194,7 @@ public class SimpleValue implements KeyValue { @Override public void createForeignKeyOfEntity(String entityName) { if ( !hasFormula() && !"none".equals(getForeignKeyName())) { - ForeignKey fk = table.createForeignKey( getForeignKeyName(), getConstraintColumns(), entityName ); + ForeignKey fk = table.createForeignKey( getForeignKeyName(), getConstraintColumns(), entityName, getForeignKeyDefinition() ); fk.setCascadeDeleteEnabled(cascadeDeleteEnabled); } } @@ -347,6 +348,14 @@ public class SimpleValue implements KeyValue { public void setForeignKeyName(String foreignKeyName) { this.foreignKeyName = foreignKeyName; } + + public String getForeignKeyDefinition() { + return foreignKeyDefinition; + } + + public void setForeignKeyDefinition(String foreignKeyDefinition) { + this.foreignKeyDefinition = foreignKeyDefinition; + } public boolean isAlternateUniqueKey() { return alternateUniqueKey; diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Table.java b/hibernate-core/src/main/java/org/hibernate/mapping/Table.java index 803ac23c16..499265e61e 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Table.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Table.java @@ -681,14 +681,15 @@ public class Table implements RelationalModel, Serializable, Exportable { public void createForeignKeys() { } - public ForeignKey createForeignKey(String keyName, List keyColumns, String referencedEntityName) { - return createForeignKey( keyName, keyColumns, referencedEntityName, null ); + public ForeignKey createForeignKey(String keyName, List keyColumns, String referencedEntityName, String keyDefinition) { + return createForeignKey( keyName, keyColumns, referencedEntityName, keyDefinition, null ); } public ForeignKey createForeignKey( String keyName, List keyColumns, String referencedEntityName, + String keyDefinition, List referencedColumns) { final ForeignKeyKey key = new ForeignKeyKey( keyColumns, referencedEntityName, referencedColumns ); @@ -697,6 +698,7 @@ public class Table implements RelationalModel, Serializable, Exportable { fk = new ForeignKey(); fk.setTable( this ); fk.setReferencedEntityName( referencedEntityName ); + fk.setKeyDefinition(keyDefinition); fk.addColumns( keyColumns.iterator() ); if ( referencedColumns != null ) { fk.addReferencedColumns( referencedColumns.iterator() ); diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardForeignKeyExporter.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardForeignKeyExporter.java index 99babdd899..c48c7d4eb8 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardForeignKeyExporter.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardForeignKeyExporter.java @@ -103,13 +103,18 @@ public class StandardForeignKeyExporter implements Exporter { final StringBuilder buffer = new StringBuilder( "alter table " ) .append( sourceTableName ) .append( - dialect.getAddForeignKeyConstraintString( - foreignKey.getName(), - columnNames, - targetTableName, - targetColumnNames, - foreignKey.isReferenceToPrimaryKey() - ) + foreignKey.getKeyDefinition() != null ? + dialect.getAddForeignKeyConstraintString( + foreignKey.getName(), + foreignKey.getKeyDefinition() + ) : + dialect.getAddForeignKeyConstraintString( + foreignKey.getName(), + columnNames, + targetTableName, + targetColumnNames, + foreignKey.isReferenceToPrimaryKey() + ) ); if ( dialect.supportsCascadeDelete() ) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/ForeignKeyMigrationTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/ForeignKeyMigrationTest.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/schemaupdate/ForeignKeyMigrationTest.java rename to hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/ForeignKeyMigrationTest.java index ccde09faa4..0418004626 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/ForeignKeyMigrationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/ForeignKeyMigrationTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.schemaupdate; +package org.hibernate.test.schemaupdate.foreignkeys; import java.util.EnumSet; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/AbstractForeignKeyDefinitionTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/AbstractForeignKeyDefinitionTest.java new file mode 100644 index 0000000000..cc6f8c87ee --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/AbstractForeignKeyDefinitionTest.java @@ -0,0 +1,81 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.schemaupdate.foreignkeys.definition; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.EnumSet; + +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.spi.MetadataImplementor; +import org.hibernate.tool.hbm2ddl.SchemaExport; +import org.hibernate.tool.schema.TargetType; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +/** + * @author Vlad MIhalcea + */ + +public abstract class AbstractForeignKeyDefinitionTest extends BaseUnitTestCase { + + private File output; + + private StandardServiceRegistry ssr; + + private MetadataImplementor metadata; + + @Before + public void setUp() throws IOException { + output = File.createTempFile( "update_script", ".sql" ); + output.deleteOnExit(); + ssr = new StandardServiceRegistryBuilder().build(); + createSchema(); + } + + @After + public void tearsDown() { + StandardServiceRegistryBuilder.destroy( ssr ); + } + + private void createSchema() { + final MetadataSources metadataSources = new MetadataSources( ssr ); + + for ( Class c : getAnnotatedClasses() ) { + metadataSources.addAnnotatedClass( c ); + } + metadata = (MetadataImplementor) metadataSources.buildMetadata(); + metadata.validate(); + new SchemaExport() + .setHaltOnError( true ) + .setOutputFile( output.getAbsolutePath() ) + .setFormat( false ) + .create( EnumSet.of( TargetType.SCRIPT ), metadata ); + } + + protected abstract Class[] getAnnotatedClasses(); + + protected abstract boolean validate(String fileContent); + + @Test + @TestForIssue(jiraKey = "HHH-10643") + public void testForeignKeyDefinitionOverridesDefaultNamingStrategy() + throws Exception { + String fileContent = new String( Files.readAllBytes( output.toPath() ) ); + assertTrue( "Script file : " + fileContent, validate( fileContent ) ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionManyToOneTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionManyToOneTest.java new file mode 100644 index 0000000000..f331424e27 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionManyToOneTest.java @@ -0,0 +1,54 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.schemaupdate.foreignkeys.definition; + +import javax.persistence.Entity; +import javax.persistence.ForeignKey; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +import org.hibernate.dialect.H2Dialect; + +import org.hibernate.testing.RequiresDialect; + +/** + * @author Vlad Mihalcea + */ +@RequiresDialect(value = H2Dialect.class) +public class ForeignKeyDefinitionManyToOneTest + extends AbstractForeignKeyDefinitionTest { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + Box.class, + Thing.class, + }; + } + + @Entity(name = "Box") + public static class Box { + @Id + public Integer id; + + @ManyToOne + @JoinColumn(foreignKey = @ForeignKey(name = "thingy", foreignKeyDefinition = "foreign key /* FK */ (thing_id) references Thing")) + public Thing thing; + } + + @Entity(name = "Thing") + public static class Thing { + @Id + public Integer id; + } + + @Override + protected boolean validate(String fileContent) { + return fileContent.contains( "/* FK */" ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionOneToManyJoinTableTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionOneToManyJoinTableTest.java new file mode 100644 index 0000000000..0a8255eb0d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionOneToManyJoinTableTest.java @@ -0,0 +1,69 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.schemaupdate.foreignkeys.definition; + +import java.util.ArrayList; +import java.util.List; +import javax.persistence.Entity; +import javax.persistence.ForeignKey; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.OneToMany; + +import org.hibernate.dialect.H2Dialect; + +import org.hibernate.testing.RequiresDialect; + +/** + * @author Vlad Mihalcea + */ +@RequiresDialect(value = H2Dialect.class) +public class ForeignKeyDefinitionOneToManyJoinTableTest + extends AbstractForeignKeyDefinitionTest { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + Box.class, + Thing.class, + }; + } + + @Entity(name = "Box") + public static class Box { + @Id + public Integer id; + } + + @Entity(name = "Thing") + public static class Thing { + @Id + public Integer id; + + @OneToMany + @JoinTable(name = "box_thing", + joinColumns = @JoinColumn(name = "box_id"), + inverseJoinColumns = @JoinColumn(name = "thing_id"), + foreignKey = @ForeignKey( + name = "thingy", + foreignKeyDefinition = "foreign key /* Thing_FK */ (thing_id) references Thing" + ), + inverseForeignKey = @ForeignKey( + name = "boxy", + foreignKeyDefinition = "foreign key /* Box_FK */ (box_id) references Box" + ) + ) + public List things = new ArrayList<>(); + } + + @Override + protected boolean validate(String fileContent) { + return fileContent.contains( "/* Thing_FK */" ) && fileContent.contains( + "/* Box_FK */" ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionOneToOneTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionOneToOneTest.java new file mode 100644 index 0000000000..6d44816437 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionOneToOneTest.java @@ -0,0 +1,54 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.schemaupdate.foreignkeys.definition; + +import javax.persistence.Entity; +import javax.persistence.ForeignKey; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; + +import org.hibernate.dialect.H2Dialect; + +import org.hibernate.testing.RequiresDialect; + +/** + * @author Vlad Mihalcea + */ +@RequiresDialect(value = H2Dialect.class) +public class ForeignKeyDefinitionOneToOneTest + extends AbstractForeignKeyDefinitionTest { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + Box.class, + Thing.class, + }; + } + + @Entity(name = "Box") + public static class Box { + @Id + public Integer id; + + @OneToOne + @JoinColumn(foreignKey = @ForeignKey(name = "thingy", foreignKeyDefinition = "foreign key /* FK */ (thing_id) references Thing")) + public Thing thing; + } + + @Entity(name = "Thing") + public static class Thing { + @Id + public Integer id; + } + + @Override + protected boolean validate(String fileContent) { + return fileContent.contains( "/* FK */" ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionSecondaryTableTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionSecondaryTableTest.java new file mode 100644 index 0000000000..3685174361 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/definition/ForeignKeyDefinitionSecondaryTableTest.java @@ -0,0 +1,56 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.schemaupdate.foreignkeys.definition; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.ForeignKey; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.SecondaryTable; + +import org.hibernate.dialect.H2Dialect; + +import org.hibernate.testing.RequiresDialect; + +/** + * @author Vlad Mihalcea + */ +@RequiresDialect(value = H2Dialect.class) +public class ForeignKeyDefinitionSecondaryTableTest + extends AbstractForeignKeyDefinitionTest { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + User.class, + }; + } + + @Entity(name = "Users") + @SecondaryTable(name = "User_details", foreignKey = @ForeignKey(name = "secondary", foreignKeyDefinition = "foreign key /* FK */ (id) references Users")) + public class User { + + @Id + @GeneratedValue + private int id; + + private String emailAddress; + + @Column(name = "SECURITY_USERNAME", table = "User_details") + private String username; + + @Column(name = "SECURITY_PASSWORD", table = "User_details") + private String password; + } + + + @Override + protected boolean validate(String fileContent) { + return fileContent.contains( "/* FK */" ); + } +}