HHH-5098 - AssertionFailure thrown when collection contains a parameterized type
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19719 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
e630fde1aa
commit
cd304f83cf
|
@ -618,6 +618,14 @@ public class AttributeFactory {
|
||||||
return member;
|
return member;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getMemberDescription() {
|
||||||
|
return determineMemberDescription( getMember() );
|
||||||
|
}
|
||||||
|
|
||||||
|
public String determineMemberDescription(Member member) {
|
||||||
|
return member.getDeclaringClass().getName() + '#' + member.getName();
|
||||||
|
}
|
||||||
|
|
||||||
public Class<Y> getJavaType() {
|
public Class<Y> getJavaType() {
|
||||||
return javaType;
|
return javaType;
|
||||||
}
|
}
|
||||||
|
@ -823,20 +831,23 @@ public class AttributeFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Class<?> getClassFromGenericArgument(java.lang.reflect.Type type) {
|
private Class<?> getClassFromGenericArgument(java.lang.reflect.Type type) {
|
||||||
Class<?> javaType;
|
if ( type instanceof Class ) {
|
||||||
Object unsafeElementType = type;
|
return (Class) type;
|
||||||
if ( unsafeElementType instanceof Class ) {
|
|
||||||
javaType = (Class) unsafeElementType;
|
|
||||||
}
|
}
|
||||||
else if ( unsafeElementType instanceof TypeVariable ) {
|
else if ( type instanceof TypeVariable ) {
|
||||||
final java.lang.reflect.Type upperBound = ( ( TypeVariable ) unsafeElementType ).getBounds()[0];
|
final java.lang.reflect.Type upperBound = ( ( TypeVariable ) type ).getBounds()[0];
|
||||||
javaType = getClassFromGenericArgument( upperBound );
|
return getClassFromGenericArgument( upperBound );
|
||||||
|
}
|
||||||
|
else if ( type instanceof ParameterizedType ) {
|
||||||
|
final java.lang.reflect.Type rawType = ( (ParameterizedType) type ).getRawType();
|
||||||
|
return getClassFromGenericArgument( rawType );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new AssertionFailure("Fail to process type argument in a generic declaration. Type: "
|
throw new AssertionFailure(
|
||||||
+ type.getClass() );
|
"Fail to process type argument in a generic declaration. Member : " + getMemberDescription()
|
||||||
|
+ " Type: " + type.getClass()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return javaType;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ValueContext getElementValueContext() {
|
public ValueContext getElementValueContext() {
|
||||||
|
|
|
@ -36,8 +36,8 @@ import javax.persistence.EntityManager;
|
||||||
import javax.persistence.EntityManagerFactory;
|
import javax.persistence.EntityManagerFactory;
|
||||||
import javax.persistence.Persistence;
|
import javax.persistence.Persistence;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.slf4j.Logger;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import org.hibernate.cfg.AnnotationConfiguration;
|
import org.hibernate.cfg.AnnotationConfiguration;
|
||||||
import org.hibernate.cfg.Environment;
|
import org.hibernate.cfg.Environment;
|
||||||
|
@ -53,8 +53,7 @@ import org.hibernate.test.annotations.HibernateTestCase;
|
||||||
* @author Hardy Ferentschik
|
* @author Hardy Ferentschik
|
||||||
*/
|
*/
|
||||||
public abstract class TestCase extends HibernateTestCase {
|
public abstract class TestCase extends HibernateTestCase {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger( TestCase.class );
|
||||||
private static final Log log = LogFactory.getLog( TestCase.class );
|
|
||||||
|
|
||||||
protected static EntityManagerFactory factory;
|
protected static EntityManagerFactory factory;
|
||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
|
|
@ -38,7 +38,10 @@ import javax.persistence.metamodel.ListAttribute;
|
||||||
import javax.persistence.metamodel.MappedSuperclassType;
|
import javax.persistence.metamodel.MappedSuperclassType;
|
||||||
import javax.persistence.metamodel.IdentifiableType;
|
import javax.persistence.metamodel.IdentifiableType;
|
||||||
|
|
||||||
|
import org.hibernate.cfg.AnnotationConfiguration;
|
||||||
|
import org.hibernate.ejb.metamodel.MetamodelImpl;
|
||||||
import org.hibernate.ejb.test.TestCase;
|
import org.hibernate.ejb.test.TestCase;
|
||||||
|
import org.hibernate.engine.SessionFactoryImplementor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Emmanuel Bernard
|
* @author Emmanuel Bernard
|
||||||
|
@ -52,6 +55,16 @@ public class MetadataTest extends TestCase {
|
||||||
assertNotNull( entityType );
|
assertNotNull( entityType );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({ "unchecked" })
|
||||||
|
public void testBuildingMetamodelWithParameterizedCollection() {
|
||||||
|
AnnotationConfiguration cfg = new AnnotationConfiguration( );
|
||||||
|
configure( cfg );
|
||||||
|
cfg.addAnnotatedClass( WithGenericCollection.class );
|
||||||
|
cfg.buildMappings();
|
||||||
|
SessionFactoryImplementor sfi = (SessionFactoryImplementor) cfg.buildSessionFactory();
|
||||||
|
MetamodelImpl.buildMetamodel( cfg.getClassMappings(), sfi );
|
||||||
|
}
|
||||||
|
|
||||||
public void testLogicalManyToOne() throws Exception {
|
public void testLogicalManyToOne() throws Exception {
|
||||||
final EntityType<JoinedManyToOneOwner> entityType = factory.getMetamodel().entity( JoinedManyToOneOwner.class );
|
final EntityType<JoinedManyToOneOwner> entityType = factory.getMetamodel().entity( JoinedManyToOneOwner.class );
|
||||||
final SingularAttribute attr = entityType.getDeclaredSingularAttribute( "house" );
|
final SingularAttribute attr = entityType.getDeclaredSingularAttribute( "house" );
|
||||||
|
@ -218,7 +231,7 @@ public class MetadataTest extends TestCase {
|
||||||
|
|
||||||
assertTrue( cat.hasVersionAttribute() );
|
assertTrue( cat.hasVersionAttribute() );
|
||||||
assertEquals( "version", cat.getVersion(Long.class).getName() );
|
assertEquals( "version", cat.getVersion(Long.class).getName() );
|
||||||
verifyDeclaredVersiobnNotPresent( cat );
|
verifyDeclaredVersionNotPresent( cat );
|
||||||
verifyDeclaredIdNotPresentAndIdPresent(cat);
|
verifyDeclaredIdNotPresentAndIdPresent(cat);
|
||||||
|
|
||||||
assertEquals( Type.PersistenceType.MAPPED_SUPERCLASS, cat.getSupertype().getPersistenceType() );
|
assertEquals( Type.PersistenceType.MAPPED_SUPERCLASS, cat.getSupertype().getPersistenceType() );
|
||||||
|
@ -229,7 +242,7 @@ public class MetadataTest extends TestCase {
|
||||||
|
|
||||||
assertTrue( cattish.hasVersionAttribute() );
|
assertTrue( cattish.hasVersionAttribute() );
|
||||||
assertEquals( "version", cattish.getVersion(Long.class).getName() );
|
assertEquals( "version", cattish.getVersion(Long.class).getName() );
|
||||||
verifyDeclaredVersiobnNotPresent( cattish );
|
verifyDeclaredVersionNotPresent( cattish );
|
||||||
verifyDeclaredIdNotPresentAndIdPresent(cattish);
|
verifyDeclaredIdNotPresentAndIdPresent(cattish);
|
||||||
|
|
||||||
assertEquals( Type.PersistenceType.ENTITY, cattish.getSupertype().getPersistenceType() );
|
assertEquals( Type.PersistenceType.ENTITY, cattish.getSupertype().getPersistenceType() );
|
||||||
|
@ -240,7 +253,7 @@ public class MetadataTest extends TestCase {
|
||||||
|
|
||||||
assertTrue( feline.hasVersionAttribute() );
|
assertTrue( feline.hasVersionAttribute() );
|
||||||
assertEquals( "version", feline.getVersion(Long.class).getName() );
|
assertEquals( "version", feline.getVersion(Long.class).getName() );
|
||||||
verifyDeclaredVersiobnNotPresent( feline );
|
verifyDeclaredVersionNotPresent( feline );
|
||||||
verifyDeclaredIdNotPresentAndIdPresent(feline);
|
verifyDeclaredIdNotPresentAndIdPresent(feline);
|
||||||
|
|
||||||
assertEquals( Type.PersistenceType.MAPPED_SUPERCLASS, feline.getSupertype().getPersistenceType() );
|
assertEquals( Type.PersistenceType.MAPPED_SUPERCLASS, feline.getSupertype().getPersistenceType() );
|
||||||
|
@ -251,7 +264,7 @@ public class MetadataTest extends TestCase {
|
||||||
|
|
||||||
assertTrue( animal.hasVersionAttribute() );
|
assertTrue( animal.hasVersionAttribute() );
|
||||||
assertEquals( "version", animal.getVersion(Long.class).getName() );
|
assertEquals( "version", animal.getVersion(Long.class).getName() );
|
||||||
verifyDeclaredVersiobnNotPresent( animal );
|
verifyDeclaredVersionNotPresent( animal );
|
||||||
assertEquals( "id", animal.getId(Long.class).getName() );
|
assertEquals( "id", animal.getId(Long.class).getName() );
|
||||||
final SingularAttribute<Animal, Long> id = animal.getDeclaredId( Long.class );
|
final SingularAttribute<Animal, Long> id = animal.getDeclaredId( Long.class );
|
||||||
assertEquals( "id", id.getName() );
|
assertEquals( "id", id.getName() );
|
||||||
|
@ -318,7 +331,7 @@ public class MetadataTest extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void verifyDeclaredVersiobnNotPresent(IdentifiableType<?> type) {
|
private void verifyDeclaredVersionNotPresent(IdentifiableType<?> type) {
|
||||||
try {
|
try {
|
||||||
type.getDeclaredVersion(Long.class);
|
type.getDeclaredVersion(Long.class);
|
||||||
fail("Should not have a declared version");
|
fail("Should not have a declared version");
|
||||||
|
|
|
@ -0,0 +1,86 @@
|
||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
|
||||||
|
* indicated by the @author tags or express copyright attribution
|
||||||
|
* statements applied by the authors. All third-party contributions are
|
||||||
|
* distributed under license by Red Hat Inc.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
package org.hibernate.ejb.test.metadata;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class has a List of mapped entity objects that are themselves parameterized.
|
||||||
|
* This class was added for JIRA issue #HHH-
|
||||||
|
*
|
||||||
|
* @author Kahli Burke
|
||||||
|
*/
|
||||||
|
@Entity
|
||||||
|
@Table(name = "WITH_GENERIC_COLLECTION")
|
||||||
|
public class WithGenericCollection<T> implements java.io.Serializable {
|
||||||
|
@Id
|
||||||
|
@Column(name = "ID")
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
@Basic(optional=false)
|
||||||
|
private double d;
|
||||||
|
|
||||||
|
@ManyToOne(optional=false)
|
||||||
|
@JoinColumn(name="PARENT_ID", insertable=false, updatable=false)
|
||||||
|
private WithGenericCollection<? extends Object> parent = null;
|
||||||
|
|
||||||
|
@OneToMany(cascade = CascadeType.ALL)
|
||||||
|
@JoinColumn(name="PARENT_ID")
|
||||||
|
private List<WithGenericCollection<? extends Object>> children = new ArrayList<WithGenericCollection<? extends Object>>();
|
||||||
|
|
||||||
|
public WithGenericCollection() {
|
||||||
|
}
|
||||||
|
|
||||||
|
//====================================================================
|
||||||
|
// getters and setters for State fields
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setD(double d) {
|
||||||
|
this.d = d;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getD() {
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<WithGenericCollection<? extends Object>> getChildren() {
|
||||||
|
return children;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setChildren(List<WithGenericCollection<? extends Object>> children) {
|
||||||
|
this.children = children;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue