Fix Criteria#getResultType() returning Object class instead of null

This commit is contained in:
Andrea Boriero 2022-01-28 15:07:18 +01:00 committed by Christian Beikov
parent 475d028981
commit b1a09d5630
3 changed files with 79 additions and 3 deletions

View File

@ -245,7 +245,7 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext,
// to indicate that we do not know. this will allow later
// calls to `SqmSelectStatement#select`, `SqmSelectStatement#multiSelect`,
// etc. to influence the result type
return new SqmSelectStatement<Object>( (Class) null, this );
return new SqmSelectStatement( Object.class, this );
}
@Override

View File

@ -193,7 +193,7 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
checkSelectionIsJpaCompliant( selection );
}
getQuerySpec().setSelection( (JpaSelection<T>) selection );
if ( getResultType() == null ) {
if ( getResultType() == Object.class ) {
setResultType( (Class<T>) selection.getJavaType() );
}
return this;
@ -209,7 +209,7 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
}
final Selection<? extends T> resultSelection;
Class<T> resultType = getResultType();
if ( resultType == null ) {
if ( resultType == Object.class ) {
setResultType( resultType = (Class<T>) Object[].class );
}
if ( Tuple.class.isAssignableFrom( resultType ) ) {

View File

@ -0,0 +1,76 @@
package org.hibernate.orm.test.jpa.compliance;
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.Id;
import jakarta.persistence.Table;
import jakarta.persistence.Tuple;
import jakarta.persistence.criteria.CriteriaQuery;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Jpa(
annotatedClasses = CrtiteriaGetResultTypeTest.Person.class
)
public class CrtiteriaGetResultTypeTest {
@Test
public void testObjectResultType(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
final CriteriaQuery query = entityManager.getCriteriaBuilder().createQuery();
final Class resultType = query.getResultType();
assertThat( resultType, notNullValue() );
assertEquals( Object.class.getName(), resultType.getName() );
}
);
}
@Test
public void testTupleResultType(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
final CriteriaQuery query = entityManager.getCriteriaBuilder().createQuery( Tuple.class );
final Class resultType = query.getResultType();
assertThat( resultType, notNullValue() );
assertEquals( Tuple.class.getName(), resultType.getName() );
}
);
}
@Test
public void testEntityResultType(EntityManagerFactoryScope scope) {
scope.inEntityManager(
entityManager -> {
final CriteriaQuery query = entityManager.getCriteriaBuilder().createQuery( Person.class );
final Class resultType = query.getResultType();
assertThat( resultType, notNullValue() );
assertEquals( Person.class.getName(), resultType.getName() );
}
);
}
@Entity(name = "Person")
@Table(name = "PERSON_TABLE")
public static class Person {
@Id
private Integer id;
private String name;
}
}