From 4de7bf20b208459fc510244ad539fa915af906db Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 22 Nov 2022 17:59:20 +0100 Subject: [PATCH] HHH-15721 Add test for issue --- .../orm/test/jpa/criteria/TreatTest.java | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/TreatTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/TreatTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/TreatTest.java new file mode 100644 index 0000000000..d9ae93c926 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/TreatTest.java @@ -0,0 +1,139 @@ +/* + * 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.orm.test.jpa.criteria; + +import java.util.List; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.From; +import jakarta.persistence.criteria.JoinType; +import jakarta.persistence.criteria.Path; +import jakarta.persistence.criteria.Predicate; +import jakarta.persistence.criteria.Root; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@Jpa( + annotatedClasses = { + TreatTest.BaseEntity.class, + TreatTest.EntityA.class, + TreatTest.EntityB.class + } +) +@TestForIssue(jiraKey = "HHH-15721") +public class TreatTest { + + @BeforeEach + public void setUp(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + EntityB b = new EntityB( 1l, "b" ); + EntityA a = new EntityA( 1l, "a", b ); + + EntityB b2 = new EntityB( 2l, "b2" ); + EntityA a2 = new EntityA( 2l, "a2", b2 ); + + EntityB b3 = new EntityB( 3l, "b3" ); + EntityA a3 = new EntityA( 3l, "a3", b3 ); + + entityManager.persist( b ); + entityManager.persist( a ); + + entityManager.persist( b2 ); + entityManager.persist( a2 ); + + entityManager.persist( b3 ); + entityManager.persist( a3 ); + } + ); + } + + @Test + public void testCriteriaQuery(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaQuery query = cb.createQuery( EntityA.class ); + Root baseEntityRoot = query.from( BaseEntity.class ); + + From entityARoot = cb.treat( baseEntityRoot, EntityA.class ); + Path entityB = entityARoot.join( "entityB", JoinType.LEFT ); + + Predicate exp = cb.or( + cb.equal( entityARoot.get( "name" ), "a2" ), + cb.equal( entityB.get( "message" ), "b" ) + ); + + query.select( cb.treat( baseEntityRoot, EntityA.class ) ).where( exp ); + List results = entityManager.createQuery( query ).getResultList(); + + assertThat( results.size() ).isEqualTo( 2 ); + } + ); + } + + @Entity(name = "BaseEntity") + @Inheritance(strategy = InheritanceType.SINGLE_TABLE) + public static abstract class BaseEntity { + @Id + private Long id; + + public BaseEntity() { + } + + public BaseEntity(Long id) { + this.id = id; + } + } + + @Entity(name = "EntityA") + public static class EntityA extends BaseEntity { + + private String name; + + @ManyToOne + private EntityB entityB; + + public EntityA() { + } + + public EntityA(Long id, String name, EntityB entityB) { + super( id ); + this.name = name; + this.entityB = entityB; + } + } + + @Entity(name = "EntityB") + public static class EntityB { + + @Id + private Long id; + + private String message; + + public EntityB() { + } + + public EntityB(Long id, String message) { + this.id = id; + this.message = message; + } + } +}