Skip to content

Commit 6a09dea

Browse files
committed
HHH-10206 - Primary key not created for a Set after loading from XML mapping file
(cherry picked from commit e71627a)
1 parent 5ba6cdc commit 6a09dea

File tree

6 files changed

+204
-3
lines changed

6 files changed

+204
-3
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public List getColumnOrFormulaElements() {
6666
return jaxbElement.getColumnOrFormula();
6767
}
6868

69+
@Override
70+
public Boolean isNullable() {
71+
return !jaxbElement.isNotNull();
72+
}
73+
6974
@Override
7075
public SizeSource getSizeSource() {
7176
return Helper.interpretSizeSource(
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.collectionpk;
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+
@TestForIssue( jiraKey = "HHH-10206" )
31+
public class CollectionPkTest extends BaseUnitTestCase {
32+
private StandardServiceRegistry ssr;
33+
34+
@Before
35+
public void before() {
36+
ssr = new StandardServiceRegistryBuilder().build();
37+
}
38+
39+
@After
40+
public void after() {
41+
if ( ssr != null ) {
42+
StandardServiceRegistryBuilder.destroy( ssr );
43+
}
44+
}
45+
46+
@Test
47+
public void testSet() {
48+
verifyPkNameUsed(
49+
"org/hibernate/test/hbm/collectionpk/person_set.hbm.xml",
50+
"primary key (group, name)"
51+
);
52+
}
53+
54+
private void verifyPkNameUsed(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+
public void testMap() {
71+
verifyPkNameUsed(
72+
"org/hibernate/test/hbm/collectionpk/person_map.hbm.xml",
73+
"primary key (group, locale)"
74+
);
75+
}
76+
77+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.index;
8+
9+
public class PersonComment {
10+
private String locale;
11+
private String value;
12+
13+
public String getLocale() {
14+
return locale;
15+
}
16+
17+
public void setLocale(String locale) {
18+
this.locale = locale;
19+
}
20+
21+
public String getValue() {
22+
return value;
23+
}
24+
25+
public void setValue(String value) {
26+
this.value = value;
27+
}
28+
}

hibernate-core/src/test/java/org/hibernate/test/hbm/index/PersonGroup.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,50 @@
66
*/
77
package org.hibernate.test.hbm.index;
88

9-
/**
10-
* @author Steve Ebersole
11-
*/
9+
import java.util.HashMap;
10+
import java.util.HashSet;
11+
import java.util.Map;
12+
import java.util.Set;
13+
1214
public class PersonGroup {
15+
private long id;
16+
private String name;
17+
private Set<Person> persons = new HashSet<Person>();
18+
private Map<String, String> comments = new HashMap<String,String>();
19+
20+
public PersonGroup(String name) {
21+
this.name = name;
22+
}
23+
24+
public long getId() {
25+
return id;
26+
}
27+
28+
public void setId(long id) {
29+
this.id = id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public Set<Person> getPersons() {
41+
return persons;
42+
}
43+
44+
public void setPersons(Set<Person> persons) {
45+
this.persons = persons;
46+
}
47+
48+
public Map<String, String> getComments() {
49+
return comments;
50+
}
51+
52+
public void setComments(Map<String, String> comments) {
53+
this.comments = comments;
54+
}
1355
}
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+
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+
<map name="comments" table="PersonGroupComment">
19+
<key column="group" foreign-key="comment_persongroup_fk"/>
20+
<map-key column="locale" type="string"/>
21+
<element column="name" type="string" />
22+
</map>
23+
</class>
24+
25+
</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.test.hbm.index">
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)