From aae8e0c10d0e55a24a22699f939f71e4e4a7dd12 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 7 Jan 2016 10:19:51 +0000 Subject: [PATCH] HHH-10420 - Add test for issue --- .../foreignkeys/ForeignKeyGenerationTest.java | 4 +- .../CrossSchemaForeignKeyGenerationTest.java | 74 +++++++++++++++++++ .../crossschema/SchemaOneEntity.java | 22 ++++++ .../crossschema/SchemaTwoEntity.java | 30 ++++++++ 4 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/crossschema/CrossSchemaForeignKeyGenerationTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/crossschema/SchemaOneEntity.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/crossschema/SchemaTwoEntity.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/ForeignKeyGenerationTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/ForeignKeyGenerationTest.java index 7b1324b4d4..7dd1547ea8 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/ForeignKeyGenerationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/ForeignKeyGenerationTest.java @@ -1,8 +1,8 @@ /* * Hibernate, Relational Persistence for Idiomatic Java * - * License: Apache License, Version 2.0 - * See the LICENSE file in the root directory or visit http://www.apache.org/licenses/LICENSE-2.0 + * 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; diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/crossschema/CrossSchemaForeignKeyGenerationTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/crossschema/CrossSchemaForeignKeyGenerationTest.java new file mode 100644 index 0000000000..5b650acc8f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/crossschema/CrossSchemaForeignKeyGenerationTest.java @@ -0,0 +1,74 @@ +/* + * 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.crossschema; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.List; + +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.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +/** + * @author Andrea Boriero + */ + +public class CrossSchemaForeignKeyGenerationTest 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(); + } + + @After + public void tearsDown() { + StandardServiceRegistryBuilder.destroy( ssr ); + } + + @Test + @TestForIssue(jiraKey = "HHH-10420") + public void testForeignKeysAreGeneratedAfterAllTheTablesAreCreated() throws Exception { + + final MetadataSources metadataSources = new MetadataSources( ssr ); + + metadataSources.addAnnotatedClass( SchemaOneEntity.class ); + metadataSources.addAnnotatedClass( SchemaTwoEntity.class ); + metadata = (MetadataImplementor) metadataSources.buildMetadata(); + metadata.validate(); + final SchemaExport schemaExport = new SchemaExport( metadata ) + .setHaltOnError( true ) + .setOutputFile( output.getAbsolutePath() ) + .setFormat( false ); + schemaExport.create( true, false ); + + final List sqlLines = Files.readAllLines( output.toPath() ); + assertThat( + "Expected alter table SCHEMA1.Child add constraint but is : " + sqlLines.get( 4 ), + sqlLines.get( 4 ).startsWith( "alter table " ), + is( true ) + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/crossschema/SchemaOneEntity.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/crossschema/SchemaOneEntity.java new file mode 100644 index 0000000000..dea35d83b5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/crossschema/SchemaOneEntity.java @@ -0,0 +1,22 @@ +/* + * 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.crossschema; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * @author Andrea Boriero + */ + +@Entity +@Table(schema = "SCHEMA1") +public class SchemaOneEntity { + @Id + private String id; +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/crossschema/SchemaTwoEntity.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/crossschema/SchemaTwoEntity.java new file mode 100644 index 0000000000..0cff64def0 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/crossschema/SchemaTwoEntity.java @@ -0,0 +1,30 @@ +/* + * 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.crossschema; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import java.util.HashSet; +import java.util.Set; + +/** + * @author Andrea Boriero + */ +@Entity +@Table(schema = "SCHEMA2") +public class SchemaTwoEntity { + + @Id + private String id; + + @OneToMany + @JoinColumn + private Set schemaOneEntities = new HashSet<>(); +}