From f5062b2aef6ccc03141a5e62555d32a2ce45c916 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Sat, 20 Apr 2024 16:27:22 +0200 Subject: [PATCH] HHH-17956 Add test for issue --- .../criteria/MultiSelectResultTypeTest.java | 114 +++++++++++++ .../sql/exec/EntityWithEmbeddedIdTest.java | 152 ++++++++++++++++++ 2 files changed, 266 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/MultiSelectResultTypeTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/sql/exec/EntityWithEmbeddedIdTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/MultiSelectResultTypeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/MultiSelectResultTypeTest.java new file mode 100644 index 0000000000..d204ef745d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/MultiSelectResultTypeTest.java @@ -0,0 +1,114 @@ +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.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@Jpa( + annotatedClasses = { + MultiSelectResultTypeTest.TestEntity.class + } +) +@JiraKey("HHH-17956") +public class MultiSelectResultTypeTest { + + @BeforeAll + public static void setUp(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + TestEntity testEntity = new TestEntity( 1, "a" ); + entityManager.persist( testEntity ); + } + ); + } + + @Test + public void testResultOfMultiSelect(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaQuery q = cb.createQuery( Integer[].class ); + Root r = q.from( TestEntity.class ); + q.multiselect( List.of( r.get( "id" ), r.get( "id" ) ) ); + List idPairs = entityManager.createQuery( q ).getResultList(); + assertThat( idPairs.size() ).isEqualTo( 1 ); + Integer[] ids = idPairs.get( 0 ); + } + ); + } + + @Test + public void testResultOfMultiSelectPrimitive(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaQuery q = cb.createQuery( int[].class ); + Root r = q.from( TestEntity.class ); + q.multiselect( List.of( r.get( "id" ), r.get( "id" ) ) ); + List idPairs = entityManager.createQuery( q ).getResultList(); + assertThat( idPairs.size() ).isEqualTo( 1 ); + int[] ids = idPairs.get( 0 ); + } + ); + } + + @Test + public void testResultOfMultiSelect2(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaQuery q = cb.createQuery( Object[].class ); + Root r = q.from( TestEntity.class ); + q.multiselect( List.of( r.get( "id" ), r.get( "name" ) ) ); + List values = entityManager.createQuery( q ).getResultList(); + assertThat( values.size() ).isEqualTo( 1 ); + Object[] value = values.get( 0 ); + Integer id = (Integer) value[0]; + String name = (String) value[1]; + } + ); + } + + @Test + public void testResultOfSelect(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaQuery q = cb.createQuery( Integer.class ); + Root r = q.from( TestEntity.class ); + q.select( r.get( "id" ) ); + List idPairs = entityManager.createQuery( q ).getResultList(); + assertThat( idPairs.size() ).isEqualTo( 1 ); + Integer id = idPairs.get( 0 ); + } + ); + } + + @Entity(name = "TestEntity") + public static class TestEntity { + @Id + private Integer id; + + private String name; + + public TestEntity() { + } + + public TestEntity(Integer id, String name) { + this.id = id; + this.name = name; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/sql/exec/EntityWithEmbeddedIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/sql/exec/EntityWithEmbeddedIdTest.java new file mode 100644 index 0000000000..675c968cf0 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/sql/exec/EntityWithEmbeddedIdTest.java @@ -0,0 +1,152 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.sql.exec; + +import java.io.Serializable; + +import org.hibernate.stat.spi.StatisticsImplementor; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.EmbeddedId; +import jakarta.persistence.Entity; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@DomainModel( + annotatedClasses = { + EntityWithEmbeddedIdTest.TestEntity.class + } +) +@SessionFactory(generateStatistics = true) +public class EntityWithEmbeddedIdTest { + + private PK entityId; + + @BeforeEach + public void setUp(SessionFactoryScope scope) { + final TestEntity entity = new TestEntity(); + entityId = new PK( 25, "Acme" ); + scope.inTransaction( + session -> { + entity.setId( entityId ); + entity.setData( "test" ); + session.save( entity ); + } + ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + sesison -> + sesison.createQuery( "delete from TestEntity" ).executeUpdate() + ); + } + + @Test + public void testHqlSelectOnlyTheEmbeddedId(SessionFactoryScope scope) { + StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); + statistics.clear(); + scope.inTransaction( + session -> { + final PK value = session.createQuery( + "select e.id FROM TestEntity e", + PK.class + ).uniqueResult(); + assertThat( value, equalTo( entityId ) ); + } + ); + assertThat( statistics.getPrepareStatementCount(), is( 1L ) ); + } + + @Entity(name = "TestEntity") + public static class TestEntity { + + @EmbeddedId + PK id; + + private String data; + + public PK getId() { + return id; + } + + public void setId(PK id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + } + + public static class PK implements Serializable { + private Integer value1; + private String value2; + + public PK() { + } + + public PK(Integer value1, String value2) { + this.value1 = value1; + this.value2 = value2; + } + + public Integer getValue1() { + return value1; + } + + public void setValue1(Integer value1) { + this.value1 = value1; + } + + public String getValue2() { + return value2; + } + + public void setValue2(String value2) { + this.value2 = value2; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + + final PK pk = (PK) o; + + if ( value1 != null ? !value1.equals( pk.getValue1() ) : pk.getValue1() != null ) { + return false; + } + return value2 != null ? value2.equals( pk.getValue2() ) : pk.getValue2() == null; + } + + @Override + public int hashCode() { + int result = value1 != null ? value1.hashCode() : 0; + result = 31 * result + ( value2 != null ? value2.hashCode() : 0 ); + return result; + } + } + +}