From a2aca9127c8dbec89b03b1369c7637709750f7b8 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 5 Dec 2022 16:03:00 +0100 Subject: [PATCH] HHH-15749 Add test for issue --- .../orm/test/jpa/criteria/GroupByTest.java | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/GroupByTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/GroupByTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/GroupByTest.java new file mode 100644 index 0000000000..45786b8964 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/GroupByTest.java @@ -0,0 +1,127 @@ +package org.hibernate.orm.test.jpa.criteria; + + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; + +@Jpa( + annotatedClasses = { + GroupByTest.Address.class, + GroupByTest.Person.class + } +) +@TestForIssue( jiraKey = "HHH-15749") +public class GroupByTest { + + @Test + public void testGroupBy(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + CriteriaQuery query = criteriaBuilder.createQuery( Person.class ); + Root root = query.from( Person.class ); + + query.groupBy( + root.get( "address1" ), + root.get( "address2" ), + root.get( "name" ) + ); + + query.select( criteriaBuilder.construct( + Person.class, + root.get( "address1" ), + root.get( "address2" ), + root.get( "name" ) + ) ); + + entityManager.createQuery( query ).getResultList(); + } + ); + } + + @Entity(name = "TestEntity") + public static class Person { + + @Id + private Integer id; + + private String name; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "address1_id", nullable = false) + private Address address1; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "address2_id", nullable = false) + private Address address2; + + public Person() { + } + + public Person(Integer id, String name, Address address1, Address address2) { + this.id = id; + this.name = name; + this.address1 = address1; + this.address2 = address2; + } + + public Person(Address address1, Address address2, String name) { + this.address1 = address1; + this.address2 = address2; + this.name = name; + } + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } + + public Address getAddress1() { + return address1; + } + + public Address getAddress2() { + return address2; + } + } + + @Entity(name = "Address") + @Table(name = "ADRESS_TABLE") + public static class Address { + @Id + private Integer id; + + private String descriptiom; + + public Address() { + } + + public Address(Integer id, String descriptiom) { + this.id = id; + this.descriptiom = descriptiom; + } + + public Integer getId() { + return id; + } + + public String getDescriptiom() { + return descriptiom; + } + } +}