HHH-9403 - AssertionFailure: Fail to process type argument in a generic declaration

This commit is contained in:
cristhiank 2018-06-10 16:23:51 -05:00 committed by Vlad Mihalcea
parent 566a23736d
commit 22973c355a
6 changed files with 172 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import java.lang.reflect.Member;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.TypeVariable; import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Iterator; import java.util.Iterator;
import javax.persistence.ManyToMany; import javax.persistence.ManyToMany;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
@ -878,6 +879,10 @@ public class AttributeFactory {
final java.lang.reflect.Type rawType = ( (ParameterizedType) type ).getRawType(); final java.lang.reflect.Type rawType = ( (ParameterizedType) type ).getRawType();
return getClassFromGenericArgument( rawType ); return getClassFromGenericArgument( rawType );
} }
else if ( type instanceof WildcardType ) {
final java.lang.reflect.Type upperBound = ( (WildcardType) type ).getUpperBounds()[0];
return getClassFromGenericArgument( upperBound );
}
else { else {
throw new AssertionFailure( throw new AssertionFailure(
"Fail to process type argument in a generic declaration. Member : " + getMemberDescription() "Fail to process type argument in a generic declaration. Member : " + getMemberDescription()

View File

@ -0,0 +1,43 @@
/*
* 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.metamodel;
import javax.persistence.metamodel.EntityType;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
import org.hibernate.test.metamodel.wildcardmodel.AbstractEntity;
import org.hibernate.test.metamodel.wildcardmodel.AbstractOwner;
import org.hibernate.test.metamodel.wildcardmodel.EntityOne;
import org.hibernate.test.metamodel.wildcardmodel.OwnerOne;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertNotNull;
public class WildcardTypeAttributeMetaModelTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
AbstractEntity.class,
AbstractOwner.class,
EntityOne.class,
OwnerOne.class
};
}
@Test
@TestForIssue(jiraKey = "HHH-9403")
public void testWildcardGenericAttributeCanBeResolved() {
doInJPA( this::entityManagerFactory, entityManager -> {
EntityType<AbstractOwner> entity = entityManager.getMetamodel().entity( AbstractOwner.class );
assertNotNull( entity );
} );
}
}

View File

@ -0,0 +1,43 @@
/*
* 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.metamodel.wildcardmodel;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class AbstractEntity {
@Id
@GeneratedValue
private Long id;
@ManyToOne()
private AbstractOwner owner;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public AbstractOwner getOwner() {
return owner;
}
public void setOwner(AbstractOwner owner) {
this.owner = owner;
}
}

View File

@ -0,0 +1,47 @@
/*
* 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.metamodel.wildcardmodel;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class AbstractOwner {
@Id
@GeneratedValue
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", targetEntity = AbstractEntity.class)
private List<? extends AbstractEntity> entities = new ArrayList<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<? extends AbstractEntity> getEntities() {
return entities;
}
public void setEntities(List<? extends AbstractEntity> entities) {
this.entities = entities;
}
}

View File

@ -0,0 +1,14 @@
/*
* 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.metamodel.wildcardmodel;
import javax.persistence.Entity;
@Entity
public class EntityOne extends AbstractEntity {
}

View File

@ -0,0 +1,20 @@
/*
* 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.metamodel.wildcardmodel;
import java.util.List;
import javax.persistence.Entity;
@Entity
public class OwnerOne extends AbstractOwner {
@SuppressWarnings("unchecked")
@Override
public List<EntityOne> getEntities() {
return (List<EntityOne>) super.getEntities();
}
}