HHH-13765 Add test showing the issue has been solved
This commit is contained in:
parent
d779632f15
commit
fa772e5cc8
|
@ -4,21 +4,17 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.orm.test.jpa.criteria;
|
package org.hibernate.orm.test.jpa.criteria;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
import jakarta.persistence.EntityManager;
|
|
||||||
import jakarta.persistence.EntityTransaction;
|
|
||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.persistence.GeneratedValue;
|
||||||
import jakarta.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Inheritance;
|
||||||
|
import jakarta.persistence.InheritanceType;
|
||||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||||
import jakarta.persistence.criteria.CriteriaQuery;
|
import jakarta.persistence.criteria.CriteriaQuery;
|
||||||
import jakarta.persistence.criteria.Join;
|
import jakarta.persistence.criteria.Join;
|
||||||
import jakarta.persistence.criteria.Root;
|
import jakarta.persistence.criteria.Root;
|
||||||
import jakarta.persistence.criteria.Subquery;
|
import jakarta.persistence.criteria.Subquery;
|
||||||
import jakarta.persistence.metamodel.EntityType;
|
import jakarta.persistence.metamodel.EntityType;
|
||||||
|
|
||||||
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
|
|
||||||
import org.hibernate.orm.test.jpa.metamodel.Thing;
|
import org.hibernate.orm.test.jpa.metamodel.Thing;
|
||||||
import org.hibernate.orm.test.jpa.metamodel.ThingWithQuantity;
|
import org.hibernate.orm.test.jpa.metamodel.ThingWithQuantity;
|
||||||
import org.hibernate.orm.test.jpa.metamodel.ThingWithQuantity_;
|
import org.hibernate.orm.test.jpa.metamodel.ThingWithQuantity_;
|
||||||
|
@ -27,279 +23,334 @@ import org.hibernate.orm.test.jpa.ql.TreatKeywordTest.JoinedEntitySubSubclass;
|
||||||
import org.hibernate.orm.test.jpa.ql.TreatKeywordTest.JoinedEntitySubSubclass2;
|
import org.hibernate.orm.test.jpa.ql.TreatKeywordTest.JoinedEntitySubSubclass2;
|
||||||
import org.hibernate.orm.test.jpa.ql.TreatKeywordTest.JoinedEntitySubclass;
|
import org.hibernate.orm.test.jpa.ql.TreatKeywordTest.JoinedEntitySubclass;
|
||||||
import org.hibernate.orm.test.jpa.ql.TreatKeywordTest.JoinedEntitySubclass2;
|
import org.hibernate.orm.test.jpa.ql.TreatKeywordTest.JoinedEntitySubclass2;
|
||||||
|
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
|
||||||
import org.hibernate.testing.orm.junit.JiraKey;
|
import org.hibernate.testing.orm.junit.JiraKey;
|
||||||
import org.junit.Assert;
|
import org.hibernate.testing.orm.junit.Jpa;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import java.util.List;
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Steve Ebersole
|
* @author Steve Ebersole
|
||||||
*/
|
*/
|
||||||
public class TreatKeywordTest extends BaseEntityManagerFunctionalTestCase {
|
@Jpa(
|
||||||
|
annotatedClasses = {
|
||||||
@Override
|
|
||||||
protected Class<?>[] getAnnotatedClasses() {
|
|
||||||
return new Class[] {
|
|
||||||
JoinedEntity.class, JoinedEntitySubclass.class, JoinedEntitySubSubclass.class,
|
JoinedEntity.class, JoinedEntitySubclass.class, JoinedEntitySubSubclass.class,
|
||||||
JoinedEntitySubclass2.class, JoinedEntitySubSubclass2.class,
|
JoinedEntitySubclass2.class, JoinedEntitySubSubclass2.class,
|
||||||
Animal.class, Elephant.class, Human.class, Thing.class, ThingWithQuantity.class,
|
Animal.class, Elephant.class, Human.class, Thing.class, ThingWithQuantity.class,
|
||||||
TreatAnimal.class, Dog.class, Dachshund.class, Greyhound.class
|
TreatKeywordTest.TreatAnimal.class, TreatKeywordTest.Dog.class, TreatKeywordTest.Dachshund.class, TreatKeywordTest.Greyhound.class,
|
||||||
};
|
TreatKeywordTest.Person.class, TreatKeywordTest.Father.class, TreatKeywordTest.Mother.class, TreatKeywordTest.Grandmother.class
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
public class TreatKeywordTest {
|
||||||
|
|
||||||
@Test
|
@BeforeAll
|
||||||
public void basicTest() {
|
public void setUp(EntityManagerFactoryScope scope) {
|
||||||
EntityManager em = getOrCreateEntityManager();
|
scope.inTransaction(
|
||||||
CriteriaBuilder builder = em.getCriteriaBuilder();
|
entityManager -> {
|
||||||
CriteriaQuery<Thing> criteria = builder.createQuery( Thing.class );
|
Animal animal = new Animal();
|
||||||
Root<Thing> root = criteria.from( Thing.class );
|
animal.setId( 100L );
|
||||||
criteria.select( root );
|
animal.setName( "2" );
|
||||||
criteria.where(
|
entityManager.persist( animal );
|
||||||
builder.equal(
|
Human human = new Human();
|
||||||
builder.treat( root, ThingWithQuantity.class ).get( ThingWithQuantity_.quantity ),
|
human.setId( 200L );
|
||||||
2
|
human.setName( "2" );
|
||||||
)
|
entityManager.persist( human );
|
||||||
|
Elephant elephant = new Elephant();
|
||||||
|
elephant.setId( 300L );
|
||||||
|
elephant.setName( "2" );
|
||||||
|
entityManager.persist( elephant );
|
||||||
|
}
|
||||||
);
|
);
|
||||||
em.createQuery( criteria ).getResultList();
|
|
||||||
em.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void basicTest2() {
|
public void basicTest(EntityManagerFactoryScope scope) {
|
||||||
EntityManager em = getOrCreateEntityManager();
|
scope.inEntityManager(
|
||||||
CriteriaBuilder builder = em.getCriteriaBuilder();
|
entityManager -> {
|
||||||
CriteriaQuery<Animal> criteria = builder.createQuery( Animal.class );
|
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||||
Root<Animal> root = criteria.from( Animal.class );
|
CriteriaQuery<Thing> criteria = builder.createQuery( Thing.class );
|
||||||
criteria.select( root );
|
Root<Thing> root = criteria.from( Thing.class );
|
||||||
criteria.where(
|
criteria.select( root );
|
||||||
builder.equal(
|
criteria.where(
|
||||||
builder.treat( root, Human.class ).get( "name" ),
|
builder.equal(
|
||||||
"2"
|
builder.treat( root, ThingWithQuantity.class ).get( ThingWithQuantity_.quantity ),
|
||||||
)
|
2
|
||||||
|
)
|
||||||
|
);
|
||||||
|
entityManager.createQuery( criteria ).getResultList();
|
||||||
|
}
|
||||||
);
|
);
|
||||||
em.createQuery( criteria ).getResultList();
|
|
||||||
em.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void treatPathClassTest() {
|
public void basicTest2(EntityManagerFactoryScope scope) {
|
||||||
EntityManager em = getOrCreateEntityManager();
|
scope.inEntityManager(
|
||||||
em.getTransaction().begin();
|
entityManager -> {
|
||||||
Animal animal = new Animal();
|
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||||
animal.setId(100L);
|
CriteriaQuery<Animal> criteria = builder.createQuery( Animal.class );
|
||||||
animal.setName("2");
|
Root<Animal> root = criteria.from( Animal.class );
|
||||||
em.persist(animal);
|
criteria.select( root );
|
||||||
Human human = new Human();
|
criteria.where(
|
||||||
human.setId(200L);
|
builder.equal(
|
||||||
human.setName("2");
|
builder.treat( root, Human.class ).get( "name" ),
|
||||||
em.persist(human);
|
"2"
|
||||||
Elephant elephant = new Elephant();
|
)
|
||||||
elephant.setId( 300L );
|
);
|
||||||
elephant.setName( "2" );
|
entityManager.createQuery( criteria ).getResultList();
|
||||||
em.persist( elephant );
|
}
|
||||||
em.getTransaction().commit();
|
|
||||||
|
|
||||||
CriteriaBuilder builder = em.getCriteriaBuilder();
|
|
||||||
CriteriaQuery<String> criteria = builder.createQuery( String.class );
|
|
||||||
Root<Animal> root = criteria.from( Animal.class );
|
|
||||||
EntityType<Animal> Animal_ = em.getMetamodel().entity(Animal.class);
|
|
||||||
criteria.select(root.get(Animal_.getSingularAttribute("name", String.class)));
|
|
||||||
|
|
||||||
criteria.where(builder.like(builder.treat(root, Human.class).get( Human_.name ), "2%"));
|
|
||||||
List<String> animalList = em.createQuery( criteria ).getResultList();
|
|
||||||
Assert.assertEquals("treat(Animal as Human) was ignored",1, animalList.size());
|
|
||||||
|
|
||||||
CriteriaQuery<Long> idCriteria = builder.createQuery( Long.class );
|
|
||||||
Root<Animal> idRoot = idCriteria.from( Animal.class );
|
|
||||||
idCriteria.select( idRoot.get( Animal_.getSingularAttribute( "id", Long.class ) ) );
|
|
||||||
|
|
||||||
idCriteria.where(
|
|
||||||
builder.like(
|
|
||||||
builder.treat( idRoot, Human.class )
|
|
||||||
.get( Human_.name ), "2%"
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
List<Long> animalIdList = em.createQuery( idCriteria ).getResultList();
|
|
||||||
Assert.assertEquals( "treat(Animal as Human) was ignored", 1, animalIdList.size() );
|
|
||||||
Assert.assertEquals( 200L, animalIdList.get( 0 ).longValue() );
|
|
||||||
|
|
||||||
em.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void treatPathClassTestHqlControl() {
|
public void treatPathClassTest(EntityManagerFactoryScope scope) {
|
||||||
EntityManager em = getOrCreateEntityManager();
|
scope.inEntityManager(
|
||||||
em.getTransaction().begin();
|
entityManager -> {
|
||||||
Animal animal = new Animal();
|
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||||
animal.setId(100L);
|
CriteriaQuery<String> criteria = builder.createQuery( String.class );
|
||||||
animal.setName("2");
|
Root<Animal> root = criteria.from( Animal.class );
|
||||||
em.persist( animal );
|
EntityType<Animal> Animal_ = entityManager.getMetamodel().entity( Animal.class );
|
||||||
Human human = new Human();
|
criteria.select( root.get( Animal_.getSingularAttribute( "name", String.class ) ) );
|
||||||
human.setId(200L);
|
|
||||||
human.setName("2");
|
|
||||||
em.persist(human);
|
|
||||||
Elephant elephant = new Elephant();
|
|
||||||
elephant.setId( 300L );
|
|
||||||
elephant.setName( "2" );
|
|
||||||
em.persist( elephant );
|
|
||||||
em.getTransaction().commit();
|
|
||||||
|
|
||||||
List<String> animalList = em.createQuery( "select a.name from Animal a where treat (a as Human).name like '2%'" ).getResultList();
|
criteria.where( builder.like( builder.treat( root, Human.class ).get( Human_.name ), "2%" ) );
|
||||||
Assert.assertEquals( "treat(Animal as Human) was ignored", 1, animalList.size() );
|
List<String> animalList = entityManager.createQuery( criteria ).getResultList();
|
||||||
|
assertThat( animalList.size() ).as( "treat(Animal as Human) was ignored" ).isEqualTo( 1 )
|
||||||
|
;
|
||||||
|
|
||||||
List<Long> animalIdList = em.createQuery( "select a.id from Animal a where treat (a as Human).name like '2%'" ).getResultList();
|
CriteriaQuery<Long> idCriteria = builder.createQuery( Long.class );
|
||||||
Assert.assertEquals("treat(Animal as Human) was ignored",1, animalList.size());
|
Root<Animal> idRoot = idCriteria.from( Animal.class );
|
||||||
Assert.assertEquals( 200L, animalIdList.get( 0 ).longValue() );
|
idCriteria.select( idRoot.get( Animal_.getSingularAttribute( "id", Long.class ) ) );
|
||||||
|
|
||||||
em.close();
|
idCriteria.where(
|
||||||
|
builder.like(
|
||||||
|
builder.treat( idRoot, Human.class )
|
||||||
|
.get( Human_.name ), "2%"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
List<Long> animalIdList = entityManager.createQuery( idCriteria ).getResultList();
|
||||||
|
assertThat( animalList.size() ).as( "treat(Animal as Human) was ignored" ).isEqualTo( 1 );
|
||||||
|
assertThat( animalIdList.get( 0 ).longValue() ).isEqualTo( 200L );
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@JiraKey( value = "HHH-9549" )
|
public void treatPathClassTestHqlControl(EntityManagerFactoryScope scope) {
|
||||||
public void treatRoot() {
|
scope.inEntityManager(
|
||||||
EntityManager em = getOrCreateEntityManager();
|
entityManager -> {
|
||||||
|
List<String> animalList = entityManager.createQuery(
|
||||||
|
"select a.name from Animal a where treat (a as Human).name like '2%'", String.class )
|
||||||
|
.getResultList();
|
||||||
|
assertThat( animalList.size() ).as( "treat(Animal as Human) was ignored" ).isEqualTo( 1 );
|
||||||
|
|
||||||
em.getTransaction().begin();
|
|
||||||
Animal animal = new Animal();
|
|
||||||
animal.setId(100L);
|
|
||||||
animal.setName("2");
|
|
||||||
em.persist(animal);
|
|
||||||
Human human = new Human();
|
|
||||||
human.setId(200L);
|
|
||||||
human.setName("2");
|
|
||||||
em.persist(human);
|
|
||||||
Elephant elephant = new Elephant();
|
|
||||||
elephant.setId( 300L );
|
|
||||||
elephant.setName( "2" );
|
|
||||||
em.persist( elephant );
|
|
||||||
em.getTransaction().commit();
|
|
||||||
|
|
||||||
CriteriaBuilder builder = em.getCriteriaBuilder();
|
List<Long> animalIdList = entityManager.createQuery(
|
||||||
CriteriaQuery<Human> criteria = builder.createQuery( Human.class );
|
"select a.id from Animal a where treat (a as Human).name like '2%'", Long.class )
|
||||||
Root<Animal> root = criteria.from( Animal.class );
|
.getResultList();
|
||||||
criteria.select( builder.treat( root, Human.class ) );
|
assertThat( animalList.size() ).as( "treat(Animal as Human) was ignored" ).isEqualTo( 1 );
|
||||||
List<Human> humans = em.createQuery( criteria ).getResultList();
|
|
||||||
Assert.assertEquals( 1, humans.size() );
|
|
||||||
|
|
||||||
em.close();
|
assertThat( animalIdList.get( 0 ).longValue() ).isEqualTo( 200L );
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@JiraKey( value = "HHH-9549" )
|
@JiraKey(value = "HHH-9549")
|
||||||
public void treatRootReturnSuperclass() {
|
public void treatRoot(EntityManagerFactoryScope scope) {
|
||||||
EntityManager em = getOrCreateEntityManager();
|
scope.inEntityManager(
|
||||||
|
entityManager -> {
|
||||||
em.getTransaction().begin();
|
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||||
Animal animal = new Animal();
|
CriteriaQuery<Human> criteria = builder.createQuery( Human.class );
|
||||||
animal.setId(100L);
|
Root<Animal> root = criteria.from( Animal.class );
|
||||||
animal.setName("2");
|
criteria.select( builder.treat( root, Human.class ) );
|
||||||
em.persist(animal);
|
List<Human> humans = entityManager.createQuery( criteria ).getResultList();
|
||||||
Human human = new Human();
|
assertThat( humans.size() ).isEqualTo( 1 );
|
||||||
human.setId(200L);
|
}
|
||||||
human.setName("2");
|
);
|
||||||
em.persist(human);
|
|
||||||
Elephant elephant = new Elephant();
|
|
||||||
elephant.setId( 300L );
|
|
||||||
elephant.setName( "2" );
|
|
||||||
em.persist( elephant );
|
|
||||||
em.getTransaction().commit();
|
|
||||||
|
|
||||||
CriteriaBuilder builder = em.getCriteriaBuilder();
|
|
||||||
CriteriaQuery<Animal> criteria = builder.createQuery( Animal.class );
|
|
||||||
Root<Animal> root = criteria.from( Animal.class );
|
|
||||||
criteria.select( builder.treat( root, Human.class ) );
|
|
||||||
List<Animal> animalsThatAreHuman = em.createQuery( criteria ).getResultList();
|
|
||||||
Assert.assertEquals( 1, animalsThatAreHuman.size() );
|
|
||||||
Assert.assertTrue( Human.class.isInstance( animalsThatAreHuman.get( 0 ) ) );
|
|
||||||
|
|
||||||
em.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSelectSubclassPropertyFromDowncast() {
|
@JiraKey(value = "HHH-9549")
|
||||||
EntityManager em = getOrCreateEntityManager();
|
public void treatRootReturnSuperclass(EntityManagerFactoryScope scope) {
|
||||||
CriteriaBuilder builder = em.getCriteriaBuilder();
|
scope.inEntityManager(
|
||||||
CriteriaQuery<Integer> criteria = builder.createQuery( Integer.class );
|
entityManager -> {
|
||||||
Root<Thing> root = criteria.from( Thing.class );
|
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||||
Root<ThingWithQuantity> subroot = builder.treat( root, ThingWithQuantity.class );
|
CriteriaQuery<Animal> criteria = builder.createQuery( Animal.class );
|
||||||
criteria.select( subroot.<Integer>get( "quantity" ) );
|
Root<Animal> root = criteria.from( Animal.class );
|
||||||
em.createQuery( criteria ).getResultList();
|
criteria.select( builder.treat( root, Human.class ) );
|
||||||
em.close();
|
List<Animal> animalsThatAreHuman = entityManager.createQuery( criteria ).getResultList();
|
||||||
|
assertThat( animalsThatAreHuman.size() ).isEqualTo( 1 );
|
||||||
|
assertThat( animalsThatAreHuman.get( 0 ) ).isInstanceOf( Human.class );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSelectSubclassPropertyFromDowncast(EntityManagerFactoryScope scope) {
|
||||||
|
scope.inEntityManager(
|
||||||
|
entityManager -> {
|
||||||
|
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<Integer> criteria = builder.createQuery( Integer.class );
|
||||||
|
Root<Thing> root = criteria.from( Thing.class );
|
||||||
|
Root<ThingWithQuantity> subroot = builder.treat( root, ThingWithQuantity.class );
|
||||||
|
criteria.select( subroot.<Integer>get( "quantity" ) );
|
||||||
|
entityManager.createQuery( criteria ).getResultList();
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@JiraKey(value = "HHH-9411")
|
@JiraKey(value = "HHH-9411")
|
||||||
public void testTreatWithRestrictionOnAbstractClass() {
|
public void testTreatWithRestrictionOnAbstractClass(EntityManagerFactoryScope scope) {
|
||||||
EntityManager em = getOrCreateEntityManager();
|
scope.inTransaction(
|
||||||
EntityTransaction entityTransaction = em.getTransaction();
|
entityManager -> {
|
||||||
entityTransaction.begin();
|
Greyhound greyhound = new Greyhound();
|
||||||
|
Dachshund dachshund = new Dachshund();
|
||||||
|
entityManager.persist( greyhound );
|
||||||
|
entityManager.persist( dachshund );
|
||||||
|
|
||||||
Greyhound greyhound = new Greyhound();
|
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||||
Dachshund dachshund = new Dachshund();
|
CriteriaQuery<TreatAnimal> criteriaQuery = cb.createQuery( TreatAnimal.class );
|
||||||
em.persist( greyhound );
|
|
||||||
em.persist( dachshund );
|
|
||||||
|
|
||||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
Root<TreatAnimal> animal = criteriaQuery.from( TreatAnimal.class );
|
||||||
CriteriaQuery<TreatAnimal> criteriaQuery = cb.createQuery( TreatAnimal.class );
|
|
||||||
|
|
||||||
Root<TreatAnimal> animal = criteriaQuery.from( TreatAnimal.class );
|
Root<Dog> dog = cb.treat( animal, Dog.class );
|
||||||
|
|
||||||
Root<Dog> dog = cb.treat( animal, Dog.class );
|
// only fast dogs
|
||||||
|
criteriaQuery.where( cb.isTrue( dog.<Boolean>get( "fast" ) ) );
|
||||||
|
|
||||||
// only fast dogs
|
List<TreatAnimal> results = entityManager.createQuery( criteriaQuery ).getResultList();
|
||||||
criteriaQuery.where( cb.isTrue( dog.<Boolean>get( "fast" ) ) );
|
|
||||||
|
|
||||||
List<TreatAnimal> results = em.createQuery( criteriaQuery ).getResultList();
|
// we should only have a single Greyhound here, not slow long dogs!
|
||||||
|
assertThat( results ).isEqualTo( List.of( greyhound ) );
|
||||||
// we should only have a single Greyhound here, not slow long dogs!
|
}
|
||||||
assertEquals( Arrays.asList( greyhound ), results );
|
);
|
||||||
|
|
||||||
entityTransaction.commit();
|
|
||||||
em.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@JiraKey(value = "HHH-16657")
|
@JiraKey(value = "HHH-16657")
|
||||||
public void testTypeFilterInSubquery() {
|
public void testTypeFilterInSubquery(EntityManagerFactoryScope scope) {
|
||||||
EntityManager em = getOrCreateEntityManager();
|
scope.inTransaction(
|
||||||
EntityTransaction entityTransaction = em.getTransaction();
|
entityManager -> {
|
||||||
entityTransaction.begin();
|
JoinedEntitySubclass2 child1 = new JoinedEntitySubclass2( 3, "child1" );
|
||||||
|
JoinedEntitySubSubclass2 child2 = new JoinedEntitySubSubclass2( 4, "child2" );
|
||||||
|
JoinedEntitySubclass root1 = new JoinedEntitySubclass( 1, "root1", child1 );
|
||||||
|
JoinedEntitySubSubclass root2 = new JoinedEntitySubSubclass( 2, "root2", child2 );
|
||||||
|
entityManager.persist( child1 );
|
||||||
|
entityManager.persist( child2 );
|
||||||
|
entityManager.persist( root1 );
|
||||||
|
entityManager.persist( root2 );
|
||||||
|
|
||||||
JoinedEntitySubclass2 child1 = new JoinedEntitySubclass2( 3, "child1");
|
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||||
JoinedEntitySubSubclass2 child2 = new JoinedEntitySubSubclass2( 4, "child2");
|
CriteriaQuery<String> query = cb.createQuery( String.class );
|
||||||
JoinedEntitySubclass root1 = new JoinedEntitySubclass( 1, "root1", child1);
|
Root<JoinedEntitySubclass> root = query.from( JoinedEntitySubclass.class );
|
||||||
JoinedEntitySubSubclass root2 = new JoinedEntitySubSubclass( 2, "root2", child2);
|
query.orderBy( cb.asc( root.get( "id" ) ) );
|
||||||
em.persist( child1 );
|
Subquery<String> subquery = query.subquery( String.class );
|
||||||
em.persist( child2 );
|
Root<JoinedEntitySubclass> subqueryRoot = subquery.correlate( root );
|
||||||
em.persist( root1 );
|
Join<Object, Object> other = subqueryRoot.join( "other" );
|
||||||
em.persist( root2 );
|
subquery.select( other.get( "name" ) );
|
||||||
|
subquery.where( cb.equal( root.type(), cb.literal( JoinedEntitySubclass.class ) ) );
|
||||||
|
query.select( subquery );
|
||||||
|
|
||||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
List<String> results = entityManager.createQuery(
|
||||||
CriteriaQuery<String> query = cb.createQuery( String.class );
|
"select (select o.name from j.other o where type(j) = JoinedEntitySubSubclass) from JoinedEntitySubclass j order by j.id",
|
||||||
Root<JoinedEntitySubclass> root = query.from( JoinedEntitySubclass.class );
|
String.class
|
||||||
query.orderBy( cb.asc( root.get( "id" ) ) );
|
).getResultList();
|
||||||
Subquery<String> subquery = query.subquery( String.class );
|
|
||||||
Root<JoinedEntitySubclass> subqueryRoot = subquery.correlate( root );
|
|
||||||
Join<Object, Object> other = subqueryRoot.join( "other" );
|
|
||||||
subquery.select( other.get( "name" ) );
|
|
||||||
subquery.where( cb.equal( root.type(), cb.literal( JoinedEntitySubclass.class ) ) );
|
|
||||||
query.select( subquery );
|
|
||||||
|
|
||||||
List<String> results = em.createQuery(
|
assertThat( results.size() ).isEqualTo( 2 );
|
||||||
"select (select o.name from j.other o where type(j) = JoinedEntitySubSubclass) from JoinedEntitySubclass j order by j.id",
|
assertThat( results.get( 0 ) ).isNull();
|
||||||
String.class
|
assertThat( results.get( 1 ) ).isEqualTo( "child2" );
|
||||||
).getResultList();
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals( 2, results.size() );
|
@Test
|
||||||
assertNull( results.get( 0 ) );
|
@JiraKey("HHH-13765")
|
||||||
assertEquals( "child2", results.get( 1 ) );
|
public void treatRootSingleTableInheritance(EntityManagerFactoryScope scope) {
|
||||||
|
scope.inTransaction(
|
||||||
|
entityManager -> {
|
||||||
|
entityManager.persist( new Person(1L, "Luigi") );
|
||||||
|
|
||||||
entityTransaction.commit();
|
entityManager.persist( new Mother(2L, "Anna") );
|
||||||
em.close();
|
|
||||||
|
entityManager.persist( new Grandmother(3L, "Elisabetta") );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
scope.inEntityManager(
|
||||||
|
entityManager -> {
|
||||||
|
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<Person> criteria = builder.createQuery( Person.class );
|
||||||
|
|
||||||
|
Root<Person> person = criteria.from( Person.class );
|
||||||
|
criteria.select( person );
|
||||||
|
criteria.where( builder.isNotNull( builder.treat( person, Mother.class ).get( "name" ) ) );
|
||||||
|
|
||||||
|
List<Person> people = entityManager.createQuery( criteria ).getResultList();
|
||||||
|
assertThat( people.size() ).isEqualTo( 2 );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||||
|
public static class Person {
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Person() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person(Long id, String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Id
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public static class Father extends Person {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public static class Mother extends Person {
|
||||||
|
|
||||||
|
public Mother() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mother(Long id, String name) {
|
||||||
|
super(id, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public static class Grandmother extends Mother {
|
||||||
|
public Grandmother() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Grandmother(Long id, String name) {
|
||||||
|
super(id, name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Entity(name = "TreatAnimal")
|
@Entity(name = "TreatAnimal")
|
||||||
|
|
Loading…
Reference in New Issue