Skip to content

Commit 6c72d0d

Browse files
committed
HHH-10169 - Add test for issue
1 parent f355d06 commit 6c72d0d

File tree

10 files changed

+291
-0
lines changed

10 files changed

+291
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.inheritance;
8+
9+
/**
10+
* @author Andrea Boriero
11+
*/
12+
public class CreditCardPayment extends Payment{
13+
private String creditCardType;
14+
15+
public String getCreditCardType() {
16+
return creditCardType;
17+
}
18+
19+
public void setCreditCardType(String creditCardType) {
20+
this.creditCardType = creditCardType;
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
<hibernate-mapping package="org.hibernate.test.schemaupdate.joinedsubclass">
12+
<joined-subclass extends="org.hibernate.test.schemaupdate.inheritance.Person" lazy="false" name="org.hibernate.test.schemaupdate.inheritance.Employee" table="EMPLOYEES">
13+
<key foreign-key="FK_EMP_PER">
14+
<column name="EMP_ID"/>
15+
</key>
16+
</joined-subclass>
17+
</hibernate-mapping>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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.inheritance;
8+
9+
import java.io.Serializable;
10+
11+
/**
12+
* @author Andrea Boriero
13+
*/
14+
public class Employee extends Person implements Serializable {
15+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.inheritance;
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.SchemaUpdate;
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-10169")
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/inheritance/Employee.hbm.xml" )
44+
.addResource( "org/hibernate/test/schemaupdate/inheritance/Person.hbm.xml" )
45+
.addResource( "org/hibernate/test/schemaupdate/inheritance/Manager.hbm.xml" )
46+
.addResource( "org/hibernate/test/schemaupdate/inheritance/Payment.hbm.xml" )
47+
.buildMetadata();
48+
metadata.validate();
49+
50+
SchemaUpdate su = new SchemaUpdate( ssr, metadata );
51+
su.setHaltOnError( true );
52+
su.setOutputFile( output.getAbsolutePath() );
53+
su.setDelimiter( ";" );
54+
su.setFormat( true );
55+
su.execute( true, false );
56+
57+
String fileContent = new String( Files.readAllBytes( output.toPath() ) );
58+
assertThat( fileContent.toLowerCase().contains( "fk_emp_per" ), is( true ) );
59+
}
60+
finally {
61+
StandardServiceRegistryBuilder.destroy( ssr );
62+
}
63+
}
64+
65+
@Test
66+
public void testSubclassForeignKeyNameIsNotAutoGeneratedWhenProvided() throws Exception {
67+
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
68+
.applySetting( Environment.HBM2DDL_AUTO, "none" )
69+
.build();
70+
try {
71+
File output = File.createTempFile( "update_script", ".sql" );
72+
output.deleteOnExit();
73+
74+
final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr )
75+
.addResource( "org/hibernate/test/schemaupdate/inheritance/Payment.hbm.xml" )
76+
.buildMetadata();
77+
metadata.validate();
78+
79+
SchemaUpdate su = new SchemaUpdate( ssr, metadata );
80+
su.setHaltOnError( true );
81+
su.setOutputFile( output.getAbsolutePath() );
82+
su.setDelimiter( ";" );
83+
su.setFormat( true );
84+
su.execute( true, false );
85+
86+
String fileContent = new String( Files.readAllBytes( output.toPath() ) );
87+
assertThat( fileContent.toLowerCase().contains( "fk_cc_pay" ), is( true ) );
88+
}
89+
finally {
90+
StandardServiceRegistryBuilder.destroy( ssr );
91+
}
92+
}
93+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
<hibernate-mapping package="org.hibernate.test.schemaupdate.joinedsubclass">
12+
<joined-subclass extends="org.hibernate.test.schemaupdate.inheritance.Person" lazy="false" name="org.hibernate.test.schemaupdate.inheritance.Manager" table="MANAGER">
13+
<key>
14+
<column name="ID"/>
15+
</key>
16+
</joined-subclass>
17+
</hibernate-mapping>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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.inheritance;
8+
9+
import java.io.Serializable;
10+
11+
/**
12+
* @author Andrea Boriero
13+
*/
14+
public class Manager extends Person implements Serializable {
15+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
<hibernate-mapping package="org.hibernate.test.schemaupdate.joinedsubclass">
12+
<class name="org.hibernate.test.schemaupdate.inheritance.Payment" table="PAYMENT">
13+
<id name="id" type="long" column="PAYMENT_ID">
14+
<generator class="native"/>
15+
</id>
16+
<discriminator column="PAYMENT_TYPE" type="string"/>
17+
<property name="amount" column="AMOUNT"/>
18+
<subclass name="org.hibernate.test.schemaupdate.inheritance.CreditCardPayment" discriminator-value="CREDIT">
19+
<join table="CREDIT_PAYMENT">
20+
<key column="PAYMENT_ID" foreign-key="FK_CC_PAY"/>
21+
<property name="creditCardType" column="CCTYPE"/>
22+
</join>
23+
</subclass>
24+
</class>
25+
</hibernate-mapping>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.inheritance;
8+
9+
/**
10+
* @author Andrea Boriero
11+
*/
12+
public class Payment {
13+
private long id;
14+
15+
private long amount;
16+
17+
public long getId() {
18+
return id;
19+
}
20+
21+
public void setId(long id) {
22+
this.id = id;
23+
}
24+
25+
public long getAmount() {
26+
return amount;
27+
}
28+
29+
public void setAmount(long amount) {
30+
this.amount = amount;
31+
}
32+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
<hibernate-mapping package="org.hibernate.test.schemaupdate.joinedsubclass">
12+
<class name="org.hibernate.test.schemaupdate.inheritance.Person" table="PERSONS">
13+
<id name="id" type="int">
14+
<column name="PER_ID"/>
15+
<generator class="native"/>
16+
</id>
17+
<property name="name">
18+
<column name="NAME"/>
19+
</property>
20+
</class>
21+
</hibernate-mapping>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.inheritance;
8+
9+
import java.io.Serializable;
10+
11+
/**
12+
* @author Andrea Boriero
13+
*/
14+
public class Person implements Serializable {
15+
16+
private int id;
17+
private String name;
18+
19+
public int getId() {
20+
return id;
21+
}
22+
23+
public void setId(int id) {
24+
this.id = id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
}

0 commit comments

Comments
 (0)