HHH-10208 - Index not considered while loading from XML mapping file

This commit is contained in:
Steve Ebersole 2015-10-26 13:14:44 -05:00
parent 83a477a0d1
commit 8bcf66631d
9 changed files with 290 additions and 4 deletions

View File

@ -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<String> 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,

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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"
);
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String> actions = new ArrayList<String>();
@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<String> getActions() {
return actions;
}
public boolean containedText(String text) {
for ( String action : actions ) {
if ( action.contains( text ) ) {
return true;
}
}
return false;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.hbm;
/**
* @author Steve Ebersole
*/
public class PersonGroup {
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
/**
* Tests specific to {@code hbm.xml} handling/binding
*/
package org.hibernate.test.hbm;

View File

@ -0,0 +1,28 @@
<?xml version="1.0"?>
<!--
~ 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.hbm">
<class name="PersonGroup">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="name" type="string" not-null="true" />
</class>
<class name="Person">
<id name="id" type="long">
<generator class="native" />
</id>
<many-to-one name="personGroup" foreign-key="person_persongroup_fk" index="person_persongroup_index" class="PersonGroup"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,21 @@
<?xml version="1.0"?>
<!--
~ 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.hbm">
<class name="Person">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="name" index="person_name_index" type="string"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,23 @@
<?xml version="1.0"?>
<!--
~ 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.hbm">
<class name="Person">
<id name="id" type="long">
<generator class="native" />
</id>
<property name="name" type="string">
<column name="name" index="person_name_index"/>
</property>
</class>
</hibernate-mapping>