HHH-12332 - Test and fix for unrelated same named collection attributes in subtypes

This commit is contained in:
Christian Beikov 2018-03-06 12:12:59 +01:00 committed by Andrea Boriero
parent 2bd1db2d4e
commit f2d77a033e
2 changed files with 73 additions and 3 deletions

View File

@ -193,8 +193,17 @@ public abstract class AbstractPropertyMapping implements PropertyMapping {
return; return;
} }
throw new IllegalStateException( "Collection mapping in abstract entity type with a type variable is unsupported! Couldn't add property '" // When we discover incompatible types, we register "null" as property type to signal that the property is not resolvable on the parent type
+ path + "' with type: " + type ); newType = null;
if ( LOG.isTraceEnabled() ) {
LOG.tracev(
"Skipped adding same named type incompatible property to base type [{0}] for property [{1}], existing type = [{2}], incoming type = [{3}]",
getEntityName(),
path,
existingType,
type
);
}
} }
else if ( type instanceof EntityType ) { else if ( type instanceof EntityType ) {
EntityType entityType1 = (EntityType) existingType; EntityType entityType1 = (EntityType) existingType;

View File

@ -26,8 +26,12 @@ package org.hibernate.test.inheritance.discriminator;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import javax.persistence.*;
import java.util.Set;
/** /**
* Test cases for joined inheritance with a discriminator column. * Test cases for joined inheritance with a discriminator column.
* *
@ -37,7 +41,15 @@ public class JoinedInheritanceTest extends BaseCoreFunctionalTestCase {
@Override @Override
protected Class<?>[] getAnnotatedClasses() { protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Polygon.class, Quadrilateral.class }; return new Class[] {
Polygon.class,
Quadrilateral.class,
BaseEntity.class,
EntityA.class,
EntityB.class,
EntityC.class,
EntityD.class
};
} }
@Test @Test
@ -63,4 +75,53 @@ public class JoinedInheritanceTest extends BaseCoreFunctionalTestCase {
s.close(); s.close();
} }
@Test
@TestForIssue( jiraKey = "HHH-12332" )
public void joinUnrelatedCollectionOnBaseType() {
final Session s = openSession();
s.getTransaction().begin();
try {
s.createQuery("from BaseEntity b join b.attributes").list();
Assert.fail("Expected a resolution exception for property 'attributes'!");
} catch (IllegalArgumentException ex) {
Assert.assertTrue(ex.getMessage().contains("could not resolve property: attributes "));
} finally {
s.getTransaction().commit();
s.close();
}
}
// Test entities for metamodel building for HHH-12332
@Entity(name = "BaseEntity")
@Inheritance(strategy = InheritanceType.JOINED)
public static class BaseEntity {
@Id
private long id;
}
@Entity(name = "EntityA")
public static class EntityA extends BaseEntity {
@OneToMany(fetch = FetchType.LAZY)
private Set<EntityC> attributes;
}
@Entity(name = "EntityB")
public static class EntityB extends BaseEntity {
@OneToMany(fetch = FetchType.LAZY)
private Set<EntityD> attributes;
}
@Entity(name = "EntityC")
public static class EntityC {
@Id
private long id;
}
@Entity(name = "EntityD")
public static class EntityD {
@Id
private long id;
}
} }