From 8c0c98fa07812da9942a713d5afc1bda634be85f Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 12 Nov 2015 20:12:51 +0000 Subject: [PATCH] HHH-10247 - Add test for issue --- .../manytomany/ForeignKeyNameTest.java | 61 +++++++++++++++++++ .../test/schemaupdate/manytomany/Group.java | 27 ++++++++ .../test/schemaupdate/manytomany/User.java | 36 +++++++++++ .../schemaupdate/manytomany/UserGroup.hbm.xml | 28 +++++++++ 4 files changed, 152 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/ForeignKeyNameTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/Group.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/User.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/UserGroup.hbm.xml diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/ForeignKeyNameTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/ForeignKeyNameTest.java new file mode 100644 index 0000000000..e3a2bf1a7a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/ForeignKeyNameTest.java @@ -0,0 +1,61 @@ +/* + * 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.manytomany; + +import java.io.File; +import java.nio.file.Files; + +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.cfg.Environment; +import org.hibernate.tool.hbm2ddl.SchemaExport; + +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 ForeignKeyNameTest extends BaseUnitTestCase { + + @Test + @TestForIssue(jiraKey = "HHH-10247") + public void testJoinedSubclassForeignKeyNameIsNotAutoGeneratedWhenProvided() throws Exception { + StandardServiceRegistry ssr = new StandardServiceRegistryBuilder() + .applySetting( Environment.HBM2DDL_AUTO, "none" ) + .build(); + try { + File output = File.createTempFile( "update_script", ".sql" ); + output.deleteOnExit(); + + final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) + .addResource( "org/hibernate/test/schemaupdate/manytomany/UserGroup.hbm.xml" ) + .buildMetadata(); + metadata.validate(); + + SchemaExport schemaExport = new SchemaExport( ssr, metadata ); + schemaExport.setOutputFile( output.getAbsolutePath() ); + schemaExport.setDelimiter( ";" ); + schemaExport.setFormat( true ); + schemaExport.create( true, false ); + + String fileContent = new String( Files.readAllBytes( output.toPath() ) ); + assertThat( fileContent.toLowerCase().contains( "fk_user_group" ), is( true ) ); + assertThat( fileContent.toLowerCase().contains( "fk_group_user" ), is( true ) ); + } + finally { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/Group.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/Group.java new file mode 100644 index 0000000000..07a54b90df --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/Group.java @@ -0,0 +1,27 @@ +/* + * 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.manytomany; + +import java.io.Serializable; + +/** + * @author Andrea Boriero + */ +public class Group implements Serializable { + + private Long id; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + +} + diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/User.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/User.java new file mode 100644 index 0000000000..85392ff522 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/User.java @@ -0,0 +1,36 @@ +/* + * 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.manytomany; + +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +/** + * @author Andrea Boriero + */ +public class User implements Serializable { + + private Long id; + private Set groups = new HashSet(); + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Set getGroups() { + return groups; + } + + public void setGroups(Set groups) { + this.groups = groups; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/UserGroup.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/UserGroup.hbm.xml new file mode 100644 index 0000000000..8e23d54b11 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/manytomany/UserGroup.hbm.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + +