Skip to content

Commit 8c0c98f

Browse files
committed
HHH-10247 - Add test for issue
1 parent 5ca5e90 commit 8c0c98f

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.schemaupdate.manytomany;
8+
9+
import java.io.File;
10+
import java.nio.file.Files;
11+
12+
import org.hibernate.boot.MetadataSources;
13+
import org.hibernate.boot.registry.StandardServiceRegistry;
14+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
15+
import org.hibernate.boot.spi.MetadataImplementor;
16+
import org.hibernate.cfg.Environment;
17+
import org.hibernate.tool.hbm2ddl.SchemaExport;
18+
19+
import org.junit.Test;
20+
21+
import org.hibernate.testing.TestForIssue;
22+
import org.hibernate.testing.junit4.BaseUnitTestCase;
23+
24+
import static org.hamcrest.core.Is.is;
25+
import static org.junit.Assert.assertThat;
26+
27+
/**
28+
* @author Andrea Boriero
29+
*/
30+
public class ForeignKeyNameTest extends BaseUnitTestCase {
31+
32+
@Test
33+
@TestForIssue(jiraKey = "HHH-10247")
34+
public void testJoinedSubclassForeignKeyNameIsNotAutoGeneratedWhenProvided() throws Exception {
35+
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
36+
.applySetting( Environment.HBM2DDL_AUTO, "none" )
37+
.build();
38+
try {
39+
File output = File.createTempFile( "update_script", ".sql" );
40+
output.deleteOnExit();
41+
42+
final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr )
43+
.addResource( "org/hibernate/test/schemaupdate/manytomany/UserGroup.hbm.xml" )
44+
.buildMetadata();
45+
metadata.validate();
46+
47+
SchemaExport schemaExport = new SchemaExport( ssr, metadata );
48+
schemaExport.setOutputFile( output.getAbsolutePath() );
49+
schemaExport.setDelimiter( ";" );
50+
schemaExport.setFormat( true );
51+
schemaExport.create( true, false );
52+
53+
String fileContent = new String( Files.readAllBytes( output.toPath() ) );
54+
assertThat( fileContent.toLowerCase().contains( "fk_user_group" ), is( true ) );
55+
assertThat( fileContent.toLowerCase().contains( "fk_group_user" ), is( true ) );
56+
}
57+
finally {
58+
StandardServiceRegistryBuilder.destroy( ssr );
59+
}
60+
}
61+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.schemaupdate.manytomany;
8+
9+
import java.io.Serializable;
10+
11+
/**
12+
* @author Andrea Boriero
13+
*/
14+
public class Group implements Serializable {
15+
16+
private Long id;
17+
18+
public Long getId() {
19+
return id;
20+
}
21+
22+
public void setId(Long id) {
23+
this.id = id;
24+
}
25+
26+
}
27+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.schemaupdate.manytomany;
8+
9+
import java.io.Serializable;
10+
import java.util.HashSet;
11+
import java.util.Set;
12+
13+
/**
14+
* @author Andrea Boriero
15+
*/
16+
public class User implements Serializable {
17+
18+
private Long id;
19+
private Set groups = new HashSet();
20+
21+
public Long getId() {
22+
return id;
23+
}
24+
25+
public void setId(Long id) {
26+
this.id = id;
27+
}
28+
29+
public Set getGroups() {
30+
return groups;
31+
}
32+
33+
public void setGroups(Set groups) {
34+
this.groups = groups;
35+
}
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ Hibernate, Relational Persistence for Idiomatic Java
4+
~
5+
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
6+
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
7+
-->
8+
<!DOCTYPE hibernate-mapping PUBLIC
9+
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
10+
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
11+
12+
<hibernate-mapping
13+
package="org.hibernate.test.schemaupdate.manytomany">
14+
15+
<class name="User" table="user">
16+
<id name="id">
17+
</id>
18+
<set name="groups" table="user_group">
19+
<key column="user_id" foreign-key="fk_user_group"/>
20+
<many-to-many class="Group" column="group_id" foreign-key="fk_group_user"/>
21+
</set>
22+
</class>
23+
24+
<class name="Group" table="Group">
25+
<id name="id"/>
26+
</class>
27+
28+
</hibernate-mapping>

0 commit comments

Comments
 (0)