HHH-17956 Add test for issue
This commit is contained in:
parent
9c4baed5ae
commit
f5062b2aef
|
@ -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<Integer[]> q = cb.createQuery( Integer[].class );
|
||||
Root<TestEntity> r = q.from( TestEntity.class );
|
||||
q.multiselect( List.of( r.get( "id" ), r.get( "id" ) ) );
|
||||
List<Integer[]> 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<int[]> q = cb.createQuery( int[].class );
|
||||
Root<TestEntity> r = q.from( TestEntity.class );
|
||||
q.multiselect( List.of( r.get( "id" ), r.get( "id" ) ) );
|
||||
List<int[]> 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<Object[]> q = cb.createQuery( Object[].class );
|
||||
Root<TestEntity> r = q.from( TestEntity.class );
|
||||
q.multiselect( List.of( r.get( "id" ), r.get( "name" ) ) );
|
||||
List<Object[]> 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<Integer> q = cb.createQuery( Integer.class );
|
||||
Root<TestEntity> r = q.from( TestEntity.class );
|
||||
q.select( r.get( "id" ) );
|
||||
List<Integer> 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue