HHH-10556 : Failure getting a PluralAttributePath for collection declared in a superclass marked @Entity
This commit is contained in:
parent
0385b1436a
commit
4a54c911c1
|
@ -0,0 +1,115 @@
|
||||||
|
/*
|
||||||
|
* 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.jpa.test.criteria;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import javax.persistence.CascadeType;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityTransaction;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
|
import javax.persistence.TypedQuery;
|
||||||
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
|
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||||
|
import org.hibernate.testing.FailureExpected;
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Janario Oliveira
|
||||||
|
* @author Gail Badner
|
||||||
|
*/
|
||||||
|
public class EntitySuperclassCollectionTest extends BaseEntityManagerFunctionalTestCase {
|
||||||
|
@Override
|
||||||
|
protected Class<?>[] getAnnotatedClasses() {
|
||||||
|
return new Class[] {
|
||||||
|
PersonBase.class, Person.class, Address.class
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue( jiraKey = "HHH-10556")
|
||||||
|
@FailureExpected( jiraKey = "HHH-10556")
|
||||||
|
public void testPerson() {
|
||||||
|
String address = "super-address";
|
||||||
|
|
||||||
|
PersonBase person = createPerson( new Person(), address );
|
||||||
|
|
||||||
|
assertAddress( person, address );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertAddress(PersonBase person, String address) {
|
||||||
|
List<Object> results = find( person.getClass(), person.id, "addresses" );
|
||||||
|
assertEquals( 1, results.size() );
|
||||||
|
|
||||||
|
assertEquals( person.addresses.get( 0 ).id, ( (Address) results.get( 0 ) ).id );
|
||||||
|
assertEquals( address, ( (Address) results.get( 0 ) ).name );
|
||||||
|
|
||||||
|
getOrCreateEntityManager().close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private PersonBase createPerson(PersonBase person, String address) {
|
||||||
|
EntityManager em = createEntityManager();
|
||||||
|
EntityTransaction tx = em.getTransaction();
|
||||||
|
tx.begin();
|
||||||
|
|
||||||
|
person.addresses.add( new Address( address ) );
|
||||||
|
person = em.merge( person );
|
||||||
|
tx.commit();
|
||||||
|
return person;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Object> find(Class<?> clazz, int id, String path) {
|
||||||
|
EntityManager em = createEntityManager();
|
||||||
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<Object> cq = cb.createQuery();
|
||||||
|
Root<?> root = cq.from( clazz );
|
||||||
|
|
||||||
|
cq.select( root.get( path ) )
|
||||||
|
.where( cb.equal( root.get( "id" ), id ) );
|
||||||
|
|
||||||
|
TypedQuery<Object> query = em.createQuery( cq );
|
||||||
|
return query.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity(name="Address")
|
||||||
|
public static class Address {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private Integer id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
protected Address() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Address(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public abstract static class PersonBase {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
Integer id;
|
||||||
|
@OneToMany(cascade = CascadeType.ALL)
|
||||||
|
List<Address> addresses = new ArrayList<Address>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity(name="Person")
|
||||||
|
public static class Person extends PersonBase {
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,6 +23,8 @@ import javax.persistence.criteria.CriteriaQuery;
|
||||||
import javax.persistence.criteria.Root;
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||||
|
import org.hibernate.testing.FailureExpected;
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -30,12 +32,13 @@ import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Janario Oliveira
|
* @author Janario Oliveira
|
||||||
|
* @author Gail Badner
|
||||||
*/
|
*/
|
||||||
public class SuperclassCollectionTest extends BaseEntityManagerFunctionalTestCase {
|
public class SuperclassCollectionTest extends BaseEntityManagerFunctionalTestCase {
|
||||||
@Override
|
@Override
|
||||||
protected Class<?>[] getAnnotatedClasses() {
|
protected Class<?>[] getAnnotatedClasses() {
|
||||||
return new Class[] {
|
return new Class[] {
|
||||||
PersonBase.class, Person.class, Address.class,
|
PersonBase.class, Person.class, OtherPerson.class, Address.class,
|
||||||
OtherSubclass.class
|
OtherSubclass.class
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -59,6 +62,18 @@ public class SuperclassCollectionTest extends BaseEntityManagerFunctionalTestCas
|
||||||
assertAddress( person, address, localAddress );
|
assertAddress( person, address, localAddress );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue( jiraKey = "HHH-10556")
|
||||||
|
@FailureExpected( jiraKey = "HHH-10556")
|
||||||
|
public void testOtherPerson() {
|
||||||
|
String address = "other-person-super-address";
|
||||||
|
String localAddress = "other-person-local-address";
|
||||||
|
|
||||||
|
PersonBase person = createPerson( new OtherPerson(), address, localAddress );
|
||||||
|
|
||||||
|
assertAddress( person, address, localAddress );
|
||||||
|
}
|
||||||
|
|
||||||
private void assertAddress(PersonBase person, String address, String localAddress) {
|
private void assertAddress(PersonBase person, String address, String localAddress) {
|
||||||
List<Object> results = find( person.getClass(), person.id, "addresses" );
|
List<Object> results = find( person.getClass(), person.id, "addresses" );
|
||||||
assertEquals( 1, results.size() );
|
assertEquals( 1, results.size() );
|
||||||
|
@ -139,6 +154,10 @@ public class SuperclassCollectionTest extends BaseEntityManagerFunctionalTestCas
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Entity(name="OtherPerson")
|
||||||
|
public static class OtherPerson extends Person {
|
||||||
|
}
|
||||||
|
|
||||||
@Entity(name="OtherSubclass")
|
@Entity(name="OtherSubclass")
|
||||||
public static class OtherSubclass extends PersonBase {
|
public static class OtherSubclass extends PersonBase {
|
||||||
@OneToMany(cascade = CascadeType.ALL)
|
@OneToMany(cascade = CascadeType.ALL)
|
||||||
|
|
Loading…
Reference in New Issue