Skip to content

Commit 1de3002

Browse files
committed
HHH-10208 - Index not considered while loading from XML mapping file
(cherry picked from commit 8bcf666)
1 parent fa7fcf5 commit 1de3002

File tree

9 files changed

+290
-4
lines changed

9 files changed

+290
-4
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import org.hibernate.boot.model.source.spi.ToolingHintContext;
4646
import org.hibernate.internal.util.StringHelper;
4747

48+
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
49+
4850
/**
4951
* @author Steve Ebersole
5052
*/
@@ -447,6 +449,7 @@ public static void processBasicAttribute(
447449
);
448450

449451
processConstraints(
452+
mappingDocument,
450453
callback,
451454
logicalTableName,
452455
basicAttributeJaxbMapping.getColumnAttribute(),
@@ -457,6 +460,7 @@ public static void processBasicAttribute(
457460
}
458461

459462
private static void processConstraints(
463+
MappingDocument mappingDocument,
460464
Callback callback,
461465
String logicalTableName,
462466
String columnAttribute,
@@ -470,13 +474,20 @@ private static void processConstraints(
470474
final Set<String> groupedUniqueKeyNameSet = splitNames( groupedUniqueKeyNames );
471475
final boolean hasGroupedUniqueKeys = !groupedUniqueKeyNameSet.isEmpty();
472476

473-
if ( hasGroupedIndexes && StringHelper.isNotEmpty( columnAttribute ) ) {
477+
if ( hasGroupedIndexes ) {
478+
if ( isNotEmpty( columnAttribute ) ) {
479+
for ( String name : groupedIndexNameSet ) {
480+
callback.registerIndexColumn( name, logicalTableName, columnAttribute );
481+
}
482+
}
483+
}
484+
if ( hasGroupedIndexes && isNotEmpty( columnAttribute ) ) {
474485
for ( String name : groupedIndexNameSet ) {
475486
callback.registerIndexColumn( name, logicalTableName, columnAttribute );
476487
}
477488
}
478489

479-
if ( hasGroupedUniqueKeys && StringHelper.isNotEmpty( columnAttribute ) ) {
490+
if ( hasGroupedUniqueKeys && isNotEmpty( columnAttribute ) ) {
480491
for ( String name : groupedUniqueKeyNameSet ) {
481492
callback.registerUniqueKeyColumn( name, logicalTableName, columnAttribute );
482493
}
@@ -488,7 +499,7 @@ private static void processConstraints(
488499
}
489500

490501
final JaxbHbmColumnType column = (JaxbHbmColumnType) oColumn;
491-
if ( StringHelper.isNotEmpty( column.getIndex() ) ) {
502+
if ( isNotEmpty( column.getIndex() ) ) {
492503
callback.registerIndexColumn( column.getIndex(), logicalTableName, column.getName() );
493504
}
494505
if ( hasGroupedIndexes ) {
@@ -497,7 +508,7 @@ private static void processConstraints(
497508
}
498509
}
499510

500-
if ( StringHelper.isNotEmpty( column.getUniqueKey() ) ) {
511+
if ( isNotEmpty( column.getUniqueKey() ) ) {
501512
callback.registerUniqueKeyColumn( column.getUniqueKey(), logicalTableName, column.getName() );
502513
}
503514
if ( hasGroupedUniqueKeys ) {
@@ -589,6 +600,7 @@ public static void processManyToOneAttribute(
589600
);
590601

591602
processConstraints(
603+
mappingDocument,
592604
callback,
593605
logicalTableName,
594606
manyToOneAttributeJaxbMapping.getColumnAttribute(),
@@ -632,6 +644,7 @@ public static void processAnyAttribute(
632644
);
633645

634646
processConstraints(
647+
mappingDocument,
635648
callback,
636649
logicalTableName,
637650
null,
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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;
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.FailureExpected;
19+
import org.hibernate.testing.TestForIssue;
20+
import org.hibernate.testing.junit4.BaseUnitTestCase;
21+
import org.hibernate.test.hbm.JournalingSchemaToolingTarget;
22+
import org.junit.After;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
26+
import static org.junit.Assert.assertTrue;
27+
28+
/**
29+
* @author Steve Ebersole
30+
*/
31+
@TestForIssue( jiraKey = "HHH-10208" )
32+
public class IndexTest extends BaseUnitTestCase {
33+
private StandardServiceRegistry ssr;
34+
35+
@Before
36+
public void before() {
37+
ssr = new StandardServiceRegistryBuilder().build();
38+
}
39+
40+
@After
41+
public void after() {
42+
if ( ssr != null ) {
43+
StandardServiceRegistryBuilder.destroy( ssr );
44+
}
45+
}
46+
47+
@Test
48+
@FailureExpected( jiraKey = "HHH-10208" )
49+
public void testOneToMany() throws Exception {
50+
verifyIndexCreated(
51+
"org/hibernate/test/hbm/index/person_manytoone.hbm.xml",
52+
"person_persongroup_index"
53+
);
54+
}
55+
56+
private void verifyIndexCreated(String mappingResource, String expectedIndexName) {
57+
final Metadata metadata = new MetadataSources( ssr )
58+
.addResource( mappingResource )
59+
.buildMetadata();
60+
61+
final SchemaCreator schemaCreator = ssr.getService( SchemaManagementTool.class ).getSchemaCreator( Collections.emptyMap() );
62+
final JournalingSchemaToolingTarget target = new JournalingSchemaToolingTarget();
63+
schemaCreator.doCreation( metadata, false, target );
64+
65+
assertTrue(
66+
"Expected index [" + expectedIndexName + "] not seen in schema creation output",
67+
target.containedText( expectedIndexName )
68+
);
69+
}
70+
71+
@Test
72+
@FailureExpected( jiraKey = "HHH-10208" )
73+
public void testProperty() throws Exception {
74+
verifyIndexCreated(
75+
"org/hibernate/test/hbm/index/person_property.hbm.xml",
76+
"person_name_index"
77+
);
78+
}
79+
80+
@Test
81+
public void testPropertyColumn() throws Exception {
82+
verifyIndexCreated(
83+
"org/hibernate/test/hbm/index/person_propertycolumn.hbm.xml",
84+
"person_name_index"
85+
);
86+
}
87+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
import org.hibernate.tool.schema.spi.Target;
13+
14+
/**
15+
* @author Steve Ebersole
16+
*/
17+
public class JournalingSchemaToolingTarget implements Target {
18+
private List<String> actions = new ArrayList<String>();
19+
20+
@Override
21+
public boolean acceptsImportScriptActions() {
22+
return false;
23+
}
24+
25+
@Override
26+
public void prepare() {
27+
28+
}
29+
30+
@Override
31+
public void accept(String action) {
32+
actions.add( action );
33+
}
34+
35+
@Override
36+
public void release() {
37+
38+
}
39+
40+
public List<String> getActions() {
41+
return actions;
42+
}
43+
44+
public boolean containedText(String text) {
45+
for ( String action : actions ) {
46+
if ( action.contains( text ) ) {
47+
return true;
48+
}
49+
}
50+
51+
return false;
52+
}
53+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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;
8+
9+
public class Person {
10+
private long id;
11+
private String name;
12+
private PersonGroup personGroup;
13+
14+
public long getId() {
15+
return id;
16+
}
17+
18+
public void setId(long id) {
19+
this.id = id;
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
26+
public void setName(String name) {
27+
this.name = name;
28+
}
29+
30+
public PersonGroup getPersonGroup() {
31+
return personGroup;
32+
}
33+
34+
public void setPersonGroup(PersonGroup personGroup) {
35+
this.personGroup = personGroup;
36+
}
37+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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;
8+
9+
/**
10+
* @author Steve Ebersole
11+
*/
12+
public class PersonGroup {
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
8+
/**
9+
* Tests specific to {@code hbm.xml} handling/binding
10+
*/
11+
package org.hibernate.test.hbm;
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 package="org.hibernate.test.hbm">
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+
<many-to-one name="personGroup" foreign-key="person_persongroup_fk" index="person_persongroup_index" class="PersonGroup"/>
26+
</class>
27+
28+
</hibernate-mapping>
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+
12+
<hibernate-mapping package="org.hibernate.test.hbm">
13+
14+
<class name="Person">
15+
<id name="id" type="long">
16+
<generator class="native" />
17+
</id>
18+
<property name="name" index="person_name_index" type="string"/>
19+
</class>
20+
21+
</hibernate-mapping>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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">
13+
14+
<class name="Person">
15+
<id name="id" type="long">
16+
<generator class="native" />
17+
</id>
18+
<property name="name" type="string">
19+
<column name="name" index="person_name_index"/>
20+
</property>
21+
</class>
22+
23+
</hibernate-mapping>

0 commit comments

Comments
 (0)