HHH-10247 - Add test for issue

This commit is contained in:
Andrea Boriero 2015-11-12 20:12:51 +00:00
parent 5ca5e90551
commit 8c0c98fa07
4 changed files with 152 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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 );
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -0,0 +1,28 @@
<?xml version="1.0"?>
<!--
~ 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.test.schemaupdate.manytomany">
<class name="User" table="user">
<id name="id">
</id>
<set name="groups" table="user_group">
<key column="user_id" foreign-key="fk_user_group"/>
<many-to-many class="Group" column="group_id" foreign-key="fk_group_user"/>
</set>
</class>
<class name="Group" table="Group">
<id name="id"/>
</class>
</hibernate-mapping>