Skip to content

Commit 5ba6cdc

Browse files
committed
HHH-10207 - Constraint name not considered for a Set while loading from XML mapping file
(cherry picked from commit c07e93b)
1 parent 3ebe0f1 commit 5ba6cdc

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import org.hibernate.boot.model.source.spi.PluralAttributeElementSourceManyToAny;
6868
import org.hibernate.boot.model.source.spi.PluralAttributeElementSourceManyToMany;
6969
import org.hibernate.boot.model.source.spi.PluralAttributeElementSourceOneToMany;
70+
import org.hibernate.boot.model.source.spi.PluralAttributeKeySource;
7071
import org.hibernate.boot.model.source.spi.PluralAttributeMapKeyManyToAnySource;
7172
import org.hibernate.boot.model.source.spi.PluralAttributeMapKeyManyToManySource;
7273
import org.hibernate.boot.model.source.spi.PluralAttributeMapKeySourceBasic;
@@ -3271,7 +3272,8 @@ protected void createBackReferences() {
32713272
}
32723273

32733274
protected void bindCollectionKey() {
3274-
final String propRef = getPluralAttributeSource().getKeySource().getReferencedPropertyName();
3275+
final PluralAttributeKeySource keySource = getPluralAttributeSource().getKeySource();
3276+
final String propRef = keySource.getReferencedPropertyName();
32753277
getCollectionBinding().setReferencedPropertyName( propRef );
32763278

32773279
final KeyValue keyVal;
@@ -3286,6 +3288,7 @@ protected void bindCollectionKey() {
32863288
getCollectionBinding().getCollectionTable(),
32873289
keyVal
32883290
);
3291+
key.setForeignKeyName( keySource.getExplicitForeignKeyName() );
32893292
key.setCascadeDeleteEnabled( getPluralAttributeSource().getKeySource().isCascadeDeleteEnabled() );
32903293

32913294
final ImplicitJoinColumnNameSource.Nature implicitNamingNature;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.hbm.fk;
8+
9+
import java.util.Collections;
10+
11+
import org.hibernate.boot.Metadata;
12+
import org.hibernate.boot.MetadataSources;
13+
import org.hibernate.boot.registry.StandardServiceRegistry;
14+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
15+
import org.hibernate.tool.schema.spi.SchemaCreator;
16+
import org.hibernate.tool.schema.spi.SchemaManagementTool;
17+
18+
import org.hibernate.testing.TestForIssue;
19+
import org.hibernate.testing.junit4.BaseUnitTestCase;
20+
import org.hibernate.test.hbm.index.JournalingSchemaToolingTarget;
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
import static org.junit.Assert.assertTrue;
26+
27+
/**
28+
* @author Steve Ebersole
29+
*/
30+
public class CollectionKeyFkNameTest extends BaseUnitTestCase {
31+
private StandardServiceRegistry ssr;
32+
33+
@Before
34+
public void before() {
35+
ssr = new StandardServiceRegistryBuilder().build();
36+
}
37+
38+
@After
39+
public void after() {
40+
if ( ssr != null ) {
41+
StandardServiceRegistryBuilder.destroy( ssr );
42+
}
43+
}
44+
45+
@Test
46+
@TestForIssue( jiraKey = "HHH-10207" )
47+
public void testExplicitFkNameOnCollectionKey() {
48+
verifyFkNameUsed(
49+
"org/hibernate/test/hbm/fk/person_set.hbm.xml",
50+
"person_persongroup_fk"
51+
);
52+
}
53+
54+
private void verifyFkNameUsed(String mappingResource, String expectedName) {
55+
final Metadata metadata = new MetadataSources( ssr )
56+
.addResource( mappingResource )
57+
.buildMetadata();
58+
59+
final SchemaCreator schemaCreator = ssr.getService( SchemaManagementTool.class ).getSchemaCreator( Collections.emptyMap() );
60+
final JournalingSchemaToolingTarget target = new JournalingSchemaToolingTarget();
61+
schemaCreator.doCreation( metadata, false, target );
62+
63+
assertTrue(
64+
"Expected foreign-key name [" + expectedName + "] not seen in schema creation output",
65+
target.containedText( expectedName )
66+
);
67+
}
68+
69+
@Test
70+
@TestForIssue( jiraKey = "HHH-10207" )
71+
public void testExplicitFkNameOnManyToOne() {
72+
verifyFkNameUsed(
73+
"org/hibernate/test/hbm/fk/person_set.hbm.xml",
74+
"person_persongroup_fk"
75+
);
76+
}
77+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 package="org.hibernate.test.hbm.index">
13+
14+
<class name="PersonGroup">
15+
<id name="id" type="long">
16+
<generator class="native" />
17+
</id>
18+
<property name="name" type="string" not-null="true" />
19+
</class>
20+
21+
<class name="Person">
22+
<id name="id" type="long">
23+
<generator class="native" />
24+
</id>
25+
<property name="name" type="string" not-null="true" />
26+
<many-to-one name="persongroup" foreign-key="person_persongroup_fk" class="PersonGroup" />
27+
</class>
28+
29+
</hibernate-mapping>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 package="org.hibernate.hibernate5.constraintname">
13+
14+
<class name="PersonGroup">
15+
<id name="id" type="long">
16+
<generator class="native" />
17+
</id>
18+
<set name="persons" table="person">
19+
<key column="group" foreign-key="person_persongroup_fk"/>
20+
<element column="name" type="string" not-null="true" />
21+
</set>
22+
</class>
23+
24+
</hibernate-mapping>

0 commit comments

Comments
 (0)