diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java index 5e1eac0c6c..b77cf23275 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java @@ -829,6 +829,11 @@ public abstract class CollectionBinder { if ( debugEnabled ) { LOG.debugf( "Binding a OneToMany: %s.%s through a foreign key", propertyHolder.getEntityName(), propertyName ); } + if ( buildingContext == null ) { + throw new AssertionFailure( + "CollectionSecondPass for oneToMany should not be called with null mappings" + ); + } org.hibernate.mapping.OneToMany oneToMany = new org.hibernate.mapping.OneToMany( buildingContext.getMetadataCollector(), collection.getOwner() ); collection.setElement( oneToMany ); oneToMany.setReferencedEntityName( collectionType.getName() ); @@ -846,16 +851,11 @@ public abstract class CollectionBinder { collection.setOrderBy( orderByFragment ); } } - - if ( buildingContext == null ) { - throw new AssertionFailure( - "CollectionSecondPass for oneToMany should not be called with null mappings" - ); - } Map joins = buildingContext.getMetadataCollector().getJoins( assocClass ); if ( associatedClass == null ) { throw new MappingException( - "Association references unmapped class: " + assocClass + String.format("Association [%s] for entity [%s] references unmapped class [%s]", + propertyName, propertyHolder.getClassName(), assocClass) ); } oneToMany.setAssociatedClass( associatedClass ); diff --git a/hibernate-core/src/test/java/org/hibernate/cfg/annotations/CollectionBinderTest.java b/hibernate-core/src/test/java/org/hibernate/cfg/annotations/CollectionBinderTest.java new file mode 100644 index 0000000000..5720400588 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/cfg/annotations/CollectionBinderTest.java @@ -0,0 +1,78 @@ +/* + * 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.cfg.annotations; + +import java.sql.SQLException; +import java.util.Map; + +import org.hibernate.MappingException; +import org.hibernate.annotations.common.reflection.XClass; +import org.hibernate.boot.spi.InFlightMetadataCollector; +import org.hibernate.boot.spi.MetadataBuildingContext; +import org.hibernate.cfg.PropertyHolder; +import org.hibernate.mapping.Collection; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.mapping.Table; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Test; + +import org.mockito.Mockito; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Test for HHH-10106 + * + * @author Vyacheslav Rarata + */ +public class CollectionBinderTest extends BaseUnitTestCase { + + @Test + @TestForIssue(jiraKey = "HHH-10106") + public void testAssociatedClassException() throws SQLException { + final Collection collection = mock(Collection.class); + final Map persistentClasses = mock(Map.class); + final XClass collectionType = mock(XClass.class); + final MetadataBuildingContext buildingContext = mock(MetadataBuildingContext.class); + final InFlightMetadataCollector inFly = mock(InFlightMetadataCollector.class); + final PersistentClass persistentClass = mock(PersistentClass.class); + final Table table = mock(Table.class); + + when(buildingContext.getMetadataCollector()).thenReturn(inFly); + when(persistentClasses.get(null)).thenReturn(null); + when(collection.getOwner()).thenReturn(persistentClass); + when(collectionType.getName()).thenReturn("List"); + when(persistentClass.getTable()).thenReturn(table); + when(table.getName()).thenReturn("Hibernate"); + + CollectionBinder collectionBinder = new CollectionBinder(false) { + @Override + protected Collection createCollection(PersistentClass persistentClass) { + return null; + } + + { + final PropertyHolder propertyHolder = Mockito.mock(PropertyHolder.class); + when(propertyHolder.getClassName()).thenReturn( CollectionBinderTest.class.getSimpleName() ); + this.propertyName = "abc"; + this.propertyHolder = propertyHolder; + } + }; + + String expectMessage = "Association [abc] for entity [CollectionBinderTest] references unmapped class [List]"; + try { + collectionBinder.bindOneToManySecondPass(collection, persistentClasses, null, collectionType, false, false, buildingContext, null); + } catch (MappingException e) { + assertEquals(expectMessage, e.getMessage()); + } + } + +}