From 0b8384c67f5c875fe1db791a5b48d5f284e9b7f9 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 28 Jun 2023 10:25:01 +0200 Subject: [PATCH] HHH-15720 Add test for issue --- ...ithDynamicInstantiationAndOrderByTest.java | 229 ++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/CriteriaWithDynamicInstantiationAndOrderByTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/CriteriaWithDynamicInstantiationAndOrderByTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/CriteriaWithDynamicInstantiationAndOrderByTest.java new file mode 100644 index 0000000000..e15797d646 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/CriteriaWithDynamicInstantiationAndOrderByTest.java @@ -0,0 +1,229 @@ +package org.hibernate.orm.test.jpa.criteria; + +import java.util.List; + +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import jakarta.persistence.Tuple; +import jakarta.persistence.criteria.CompoundSelection; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@Jpa( + annotatedClasses = { + CriteriaWithDynamicInstantiationAndOrderByTest.Item.class + } +) +@JiraKey("HHH-15720") +public class CriteriaWithDynamicInstantiationAndOrderByTest { + + private static final Long ITEM_ID = 1l; + private static final String ITEM_NAME = "cisel"; + + @BeforeAll + public void setUp(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + Item item = new Item( ITEM_ID, ITEM_NAME ); + entityManager.persist( item ); + } + ); + } + + @Test + public void testWorks1ParamOrder(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> { + CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); + CriteriaQuery critQuery = criteriaBuilder.createTupleQuery(); + Root root = critQuery.from( Item.class ); + critQuery.multiselect( criteriaBuilder.construct( R1.class, root.get( "id" ) ) ); + critQuery.orderBy( criteriaBuilder.asc( root.get( "id" ) ) ); + + List results = em.createQuery( critQuery ).getResultList(); + Object o = results.get( 0 ).get( 0 ); + assertThat(o).isInstanceOf( R1.class ); + assertThat( ((R1)o).getA() ).isEqualTo( ITEM_ID ); + } + ); + } + + @Test + public void testWorks2ParamsNoOrder(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> { + CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); + CriteriaQuery critQuery = criteriaBuilder.createTupleQuery(); + Root root = critQuery.from( Item.class ); + critQuery.multiselect( criteriaBuilder.construct( + R2.class, + root.get( "id" ), + root.get( "name" ) + ) ); + + List results = em.createQuery( critQuery ).getResultList(); + Object o = results.get( 0 ).get( 0 ); + assertThat(o).isInstanceOf( R2.class ); + assertThat( ((R2)o).getA() ).isEqualTo( ITEM_ID ); + assertThat( ((R2)o).getB() ).isEqualTo( ITEM_NAME ); + } + ); + } + + @Test + public void testFail2ParamsOrder(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> { + CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); + CriteriaQuery critQuery = criteriaBuilder.createTupleQuery(); + Root root = critQuery.from( Item.class ); + critQuery.multiselect( criteriaBuilder.construct( + R2.class, + root.get( "id" ), + root.get( "name" ) + ) ); + critQuery.orderBy( criteriaBuilder.asc( root.get( "id" ) ) ); + + List results = em.createQuery( critQuery ).getResultList(); + Object o = results.get( 0 ).get( 0 ); + assertThat(o).isInstanceOf( R2.class ); + assertThat( ((R2)o).getA() ).isEqualTo( ITEM_ID ); + assertThat( ((R2)o).getB() ).isEqualTo( ITEM_NAME ); + } + ); + } + + @Test + public void test3MultiSelectParamsOrder(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> { + CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); + CriteriaQuery critQuery = criteriaBuilder.createTupleQuery(); + Root root = critQuery.from( Item.class ); + critQuery.multiselect( criteriaBuilder.construct( + R3.class, + root.get( "id" ), + root.get( "name" ), + root.get( "name" ) + ) ); + critQuery.orderBy( criteriaBuilder.asc( root.get( "id" ) ) ); + + List results = em.createQuery( critQuery ).getResultList(); + Object o = results.get( 0 ).get( 0 ); + assertThat(o).isInstanceOf( R3.class ); + assertThat( ((R3)o).getA() ).isEqualTo( ITEM_ID ); + assertThat( ((R3)o).getB() ).isEqualTo( ITEM_NAME ); + assertThat( ((R3)o).getC() ).isEqualTo( ITEM_NAME ); + } + ); + } + + @Test + public void test3SelectParamsOrder(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> { + CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); + CriteriaQuery critQuery = criteriaBuilder.createQuery(); + Root root = critQuery.from( Item.class ); + CompoundSelection construct = criteriaBuilder.construct( + R3.class, + root.get( "id" ), + root.get( "name" ), + root.get( "name" ) + ); + critQuery.select( construct ); + critQuery.orderBy( criteriaBuilder.asc( root.get( "id" ) ) ); + + List results = em.createQuery( critQuery ).getResultList(); + R3 result = results.get( 0 ); + assertThat( result.getA() ).isEqualTo( ITEM_ID ); + assertThat( result.getB() ).isEqualTo( ITEM_NAME ); + assertThat( result.getC() ).isEqualTo( ITEM_NAME ); + } + ); + } + + @Entity(name = "Item") + @Table(name = "ITEM_TABLE") + public static class Item { + @Id + private Long id; + + private String name; + + public Item() { + } + + public Item(Long id, String name) { + this.id = id; + this.name = name; + } + } + + + public static class R1 { + private Long a; + + public R1(Long a) { + this.a = a; + } + + public Long getA() { + return a; + } + } + + public static class R2 { + private Long a; + private String b; + + public R2(Long a, String b) { + this.a = a; + this.b = b; + } + + public Long getA() { + return a; + } + + public String getB() { + return b; + } + } + + public static class R3 { + private Long a; + private String b; + private String c; + + public R3(Long a, String b, String c) { + this.a = a; + this.b = b; + this.c = c; + } + + public Long getA() { + return a; + } + + public String getB() { + return b; + } + + public String getC() { + return c; + } + } + + +}