diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/AttributesHelper.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/AttributesHelper.java index 7ff95b25c5..5696845272 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/AttributesHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/AttributesHelper.java @@ -45,6 +45,8 @@ import org.hibernate.boot.model.source.spi.SingularAttributeSourceEmbedded; import org.hibernate.boot.model.source.spi.ToolingHintContext; import org.hibernate.internal.util.StringHelper; +import static org.hibernate.internal.util.StringHelper.isNotEmpty; + /** * @author Steve Ebersole */ @@ -447,6 +449,7 @@ public class AttributesHelper { ); processConstraints( + mappingDocument, callback, logicalTableName, basicAttributeJaxbMapping.getColumnAttribute(), @@ -457,6 +460,7 @@ public class AttributesHelper { } private static void processConstraints( + MappingDocument mappingDocument, Callback callback, String logicalTableName, String columnAttribute, @@ -470,13 +474,20 @@ public class AttributesHelper { final Set groupedUniqueKeyNameSet = splitNames( groupedUniqueKeyNames ); final boolean hasGroupedUniqueKeys = !groupedUniqueKeyNameSet.isEmpty(); - if ( hasGroupedIndexes && StringHelper.isNotEmpty( columnAttribute ) ) { + if ( hasGroupedIndexes ) { + if ( isNotEmpty( columnAttribute ) ) { + for ( String name : groupedIndexNameSet ) { + callback.registerIndexColumn( name, logicalTableName, columnAttribute ); + } + } + } + if ( hasGroupedIndexes && isNotEmpty( columnAttribute ) ) { for ( String name : groupedIndexNameSet ) { callback.registerIndexColumn( name, logicalTableName, columnAttribute ); } } - if ( hasGroupedUniqueKeys && StringHelper.isNotEmpty( columnAttribute ) ) { + if ( hasGroupedUniqueKeys && isNotEmpty( columnAttribute ) ) { for ( String name : groupedUniqueKeyNameSet ) { callback.registerUniqueKeyColumn( name, logicalTableName, columnAttribute ); } @@ -488,7 +499,7 @@ public class AttributesHelper { } final JaxbHbmColumnType column = (JaxbHbmColumnType) oColumn; - if ( StringHelper.isNotEmpty( column.getIndex() ) ) { + if ( isNotEmpty( column.getIndex() ) ) { callback.registerIndexColumn( column.getIndex(), logicalTableName, column.getName() ); } if ( hasGroupedIndexes ) { @@ -497,7 +508,7 @@ public class AttributesHelper { } } - if ( StringHelper.isNotEmpty( column.getUniqueKey() ) ) { + if ( isNotEmpty( column.getUniqueKey() ) ) { callback.registerUniqueKeyColumn( column.getUniqueKey(), logicalTableName, column.getName() ); } if ( hasGroupedUniqueKeys ) { @@ -589,6 +600,7 @@ public class AttributesHelper { ); processConstraints( + mappingDocument, callback, logicalTableName, manyToOneAttributeJaxbMapping.getColumnAttribute(), @@ -632,6 +644,7 @@ public class AttributesHelper { ); processConstraints( + mappingDocument, callback, logicalTableName, null, diff --git a/hibernate-core/src/test/java/org/hibernate/test/hbm/IndexTest.java b/hibernate-core/src/test/java/org/hibernate/test/hbm/IndexTest.java new file mode 100644 index 0000000000..f80df0568f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/hbm/IndexTest.java @@ -0,0 +1,87 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.hbm; + +import java.util.Collections; + +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.tool.schema.spi.SchemaCreator; +import org.hibernate.tool.schema.spi.SchemaManagementTool; + +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.hibernate.test.hbm.JournalingSchemaToolingTarget; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +/** + * @author Steve Ebersole + */ +@TestForIssue( jiraKey = "HHH-10208" ) +public class IndexTest extends BaseUnitTestCase { + private StandardServiceRegistry ssr; + + @Before + public void before() { + ssr = new StandardServiceRegistryBuilder().build(); + } + + @After + public void after() { + if ( ssr != null ) { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + @Test + @FailureExpected( jiraKey = "HHH-10208" ) + public void testOneToMany() throws Exception { + verifyIndexCreated( + "org/hibernate/test/hbm/index/person_manytoone.hbm.xml", + "person_persongroup_index" + ); + } + + private void verifyIndexCreated(String mappingResource, String expectedIndexName) { + final Metadata metadata = new MetadataSources( ssr ) + .addResource( mappingResource ) + .buildMetadata(); + + final SchemaCreator schemaCreator = ssr.getService( SchemaManagementTool.class ).getSchemaCreator( Collections.emptyMap() ); + final JournalingSchemaToolingTarget target = new JournalingSchemaToolingTarget(); + schemaCreator.doCreation( metadata, false, target ); + + assertTrue( + "Expected index [" + expectedIndexName + "] not seen in schema creation output", + target.containedText( expectedIndexName ) + ); + } + + @Test + @FailureExpected( jiraKey = "HHH-10208" ) + public void testProperty() throws Exception { + verifyIndexCreated( + "org/hibernate/test/hbm/index/person_property.hbm.xml", + "person_name_index" + ); + } + + @Test + public void testPropertyColumn() throws Exception { + verifyIndexCreated( + "org/hibernate/test/hbm/index/person_propertycolumn.hbm.xml", + "person_name_index" + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/hbm/JournalingSchemaToolingTarget.java b/hibernate-core/src/test/java/org/hibernate/test/hbm/JournalingSchemaToolingTarget.java new file mode 100644 index 0000000000..885b51e4e5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/hbm/JournalingSchemaToolingTarget.java @@ -0,0 +1,53 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.hbm; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.tool.schema.spi.Target; + +/** + * @author Steve Ebersole + */ +public class JournalingSchemaToolingTarget implements Target { + private List actions = new ArrayList(); + + @Override + public boolean acceptsImportScriptActions() { + return false; + } + + @Override + public void prepare() { + + } + + @Override + public void accept(String action) { + actions.add( action ); + } + + @Override + public void release() { + + } + + public List getActions() { + return actions; + } + + public boolean containedText(String text) { + for ( String action : actions ) { + if ( action.contains( text ) ) { + return true; + } + } + + return false; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/hbm/Person.java b/hibernate-core/src/test/java/org/hibernate/test/hbm/Person.java new file mode 100644 index 0000000000..0443e84a26 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/hbm/Person.java @@ -0,0 +1,37 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.hbm; + +public class Person { + private long id; + private String name; + private PersonGroup personGroup; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public PersonGroup getPersonGroup() { + return personGroup; + } + + public void setPersonGroup(PersonGroup personGroup) { + this.personGroup = personGroup; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/hbm/PersonGroup.java b/hibernate-core/src/test/java/org/hibernate/test/hbm/PersonGroup.java new file mode 100644 index 0000000000..07fd5fa4e6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/hbm/PersonGroup.java @@ -0,0 +1,13 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.hbm; + +/** + * @author Steve Ebersole + */ +public class PersonGroup { +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/hbm/package-info.java b/hibernate-core/src/test/java/org/hibernate/test/hbm/package-info.java new file mode 100644 index 0000000000..d09d80b872 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/hbm/package-info.java @@ -0,0 +1,11 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ + +/** + * Tests specific to {@code hbm.xml} handling/binding + */ +package org.hibernate.test.hbm; diff --git a/hibernate-core/src/test/resources/org/hibernate/test/hbm/index/person_manytoone.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/test/hbm/index/person_manytoone.hbm.xml new file mode 100644 index 0000000000..d92c379072 --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/test/hbm/index/person_manytoone.hbm.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hibernate-core/src/test/resources/org/hibernate/test/hbm/index/person_property.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/test/hbm/index/person_property.hbm.xml new file mode 100644 index 0000000000..e52e0b8f41 --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/test/hbm/index/person_property.hbm.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hibernate-core/src/test/resources/org/hibernate/test/hbm/index/person_propertycolumn.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/test/hbm/index/person_propertycolumn.hbm.xml new file mode 100644 index 0000000000..9935d89296 --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/test/hbm/index/person_propertycolumn.hbm.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + \ No newline at end of file