HHH-10207 - Constraint name not considered for a Set while loading from XML mapping file

This commit is contained in:
Steve Ebersole 2015-10-26 19:44:15 -05:00
parent 53dfed4496
commit c07e93bfb1
4 changed files with 134 additions and 1 deletions

View File

@ -67,6 +67,7 @@ import org.hibernate.boot.model.source.spi.PluralAttributeElementSourceEmbedded;
import org.hibernate.boot.model.source.spi.PluralAttributeElementSourceManyToAny;
import org.hibernate.boot.model.source.spi.PluralAttributeElementSourceManyToMany;
import org.hibernate.boot.model.source.spi.PluralAttributeElementSourceOneToMany;
import org.hibernate.boot.model.source.spi.PluralAttributeKeySource;
import org.hibernate.boot.model.source.spi.PluralAttributeMapKeyManyToAnySource;
import org.hibernate.boot.model.source.spi.PluralAttributeMapKeyManyToManySource;
import org.hibernate.boot.model.source.spi.PluralAttributeMapKeySourceBasic;
@ -3271,7 +3272,8 @@ public class ModelBinder {
}
protected void bindCollectionKey() {
final String propRef = getPluralAttributeSource().getKeySource().getReferencedPropertyName();
final PluralAttributeKeySource keySource = getPluralAttributeSource().getKeySource();
final String propRef = keySource.getReferencedPropertyName();
getCollectionBinding().setReferencedPropertyName( propRef );
final KeyValue keyVal;
@ -3286,6 +3288,7 @@ public class ModelBinder {
getCollectionBinding().getCollectionTable(),
keyVal
);
key.setForeignKeyName( keySource.getExplicitForeignKeyName() );
key.setCascadeDeleteEnabled( getPluralAttributeSource().getKeySource().isCascadeDeleteEnabled() );
final ImplicitJoinColumnNameSource.Nature implicitNamingNature;

View File

@ -0,0 +1,77 @@
/*
* 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.fk;
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.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.test.hbm.index.JournalingSchemaToolingTarget;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
/**
* @author Steve Ebersole
*/
public class CollectionKeyFkNameTest extends BaseUnitTestCase {
private StandardServiceRegistry ssr;
@Before
public void before() {
ssr = new StandardServiceRegistryBuilder().build();
}
@After
public void after() {
if ( ssr != null ) {
StandardServiceRegistryBuilder.destroy( ssr );
}
}
@Test
@TestForIssue( jiraKey = "HHH-10207" )
public void testExplicitFkNameOnCollectionKey() {
verifyFkNameUsed(
"org/hibernate/test/hbm/fk/person_set.hbm.xml",
"person_persongroup_fk"
);
}
private void verifyFkNameUsed(String mappingResource, String expectedName) {
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 foreign-key name [" + expectedName + "] not seen in schema creation output",
target.containedText( expectedName )
);
}
@Test
@TestForIssue( jiraKey = "HHH-10207" )
public void testExplicitFkNameOnManyToOne() {
verifyFkNameUsed(
"org/hibernate/test/hbm/fk/person_set.hbm.xml",
"person_persongroup_fk"
);
}
}

View File

@ -0,0 +1,29 @@
<?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.index">
<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>
<property name="name" type="string" not-null="true" />
<many-to-one name="persongroup" foreign-key="person_persongroup_fk" class="PersonGroup" />
</class>
</hibernate-mapping>

View File

@ -0,0 +1,24 @@
<?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.hibernate5.constraintname">
<class name="PersonGroup">
<id name="id" type="long">
<generator class="native" />
</id>
<set name="persons" table="person">
<key column="group" foreign-key="person_persongroup_fk"/>
<element column="name" type="string" not-null="true" />
</set>
</class>
</hibernate-mapping>