HHH-14923 - Allow CriteriaQuery to determine its "return type" as part of setting the selection

This commit is contained in:
Steve Ebersole 2021-11-09 18:54:18 -06:00
parent 2d38df66fd
commit f935d2b8e1
3 changed files with 249 additions and 6 deletions

View File

@ -71,6 +71,9 @@ import org.hibernate.query.sql.spi.NativeQueryImplementor;
import org.hibernate.query.sqm.internal.QuerySqmImpl;
import org.hibernate.query.sqm.tree.SqmStatement;
import org.hibernate.query.sqm.tree.delete.SqmDeleteStatement;
import org.hibernate.query.sqm.tree.select.SqmQueryGroup;
import org.hibernate.query.sqm.tree.select.SqmQuerySpec;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.query.sqm.tree.update.SqmUpdateStatement;
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
@ -990,11 +993,17 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
checkOpen();
try {
return new QuerySqmImpl<>(
(SqmStatement<T>) criteriaQuery,
criteriaQuery.getResultType(),
this
);
final SqmSelectStatement<T> selectStatement = (SqmSelectStatement<T>) criteriaQuery;
if ( ! ( selectStatement.getQueryPart() instanceof SqmQueryGroup ) ) {
final SqmQuerySpec<T> querySpec = selectStatement.getQuerySpec();
if ( querySpec.getSelectClause().getSelections().isEmpty() ) {
if ( querySpec.getFromClause().getRoots().size() == 1 ) {
querySpec.getSelectClause().setSelection( querySpec.getFromClause().getRoots().get(0) );
}
}
}
return new QuerySqmImpl<>( selectStatement, criteriaQuery.getResultType(), this );
}
catch ( RuntimeException e ) {
throw getExceptionConverter().convert( e );

View File

@ -224,9 +224,14 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext,
// for potential future use
}
@SuppressWarnings("unchecked,rawtypes")
@Override
public SqmSelectStatement<Object> createQuery() {
return new SqmSelectStatement<>( Object.class, this );
// IMPORTANT: we want to pass null here for the result-type
// 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 );
}
@Override

View File

@ -0,0 +1,229 @@
/*
* 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.query.criteria;
import java.util.List;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.testing.orm.domain.gambit.BasicEntity;
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.Tuple;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import jakarta.persistence.criteria.Selection;
import jakarta.persistence.metamodel.EntityType;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Steve Ebersole
*/
@DomainModel( annotatedClasses = BasicEntity.class )
@SessionFactory
public class MultiSelectTests {
@Test
public void simpleArrayTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
final CriteriaQuery criteria = nodeBuilder.createQuery();
final Root<BasicEntity> root = criteria.from( BasicEntity.class );
final EntityType<BasicEntity> model = root.getModel();
criteria.select(
nodeBuilder.array(
root.get( model.getDeclaredSingularAttribute( "id", Integer.class ) ),
root.get( model.getDeclaredSingularAttribute( "data", String.class ) )
)
);
final List<Object[]> results = session.createQuery( criteria ).list();
assertThat( results ).hasSize( 1 );
final Object[] firstResult = results.get( 0 );
assertThat( firstResult[0] ).isEqualTo( 1 );
assertThat( firstResult[1] ).isEqualTo( "abc" );
} );
}
@Test
public void multiselectArrayTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
final CriteriaQuery criteria = nodeBuilder.createQuery();
final Root<BasicEntity> root = criteria.from( BasicEntity.class );
final EntityType<BasicEntity> model = root.getModel();
criteria.multiselect(
root.get( model.getDeclaredSingularAttribute( "id", Integer.class ) ),
root.get( model.getDeclaredSingularAttribute( "data", String.class ) )
);
final List<Object[]> results = session.createQuery( criteria ).list();
assertThat( results ).hasSize( 1 );
final Object[] firstResult = results.get( 0 );
assertThat( firstResult[0] ).isEqualTo( 1 );
assertThat( firstResult[1] ).isEqualTo( "abc" );
} );
}
@Test
public void typedArrayTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
final CriteriaQuery<Object[]> criteria = nodeBuilder.createQuery(Object[].class);
final Root<BasicEntity> root = criteria.from( BasicEntity.class );
final EntityType<BasicEntity> model = root.getModel();
criteria.select(
nodeBuilder.array(
root.get( model.getDeclaredSingularAttribute( "id", Integer.class ) ),
root.get( model.getDeclaredSingularAttribute( "data", String.class ) )
)
);
final List<Object[]> results = session.createQuery( criteria ).list();
assertThat( results ).hasSize( 1 );
final Object[] firstResult = results.get( 0 );
assertThat( firstResult[0] ).isEqualTo( 1 );
assertThat( firstResult[1] ).isEqualTo( "abc" );
} );
}
@Test
public void simpleTupleTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
final CriteriaQuery<Tuple> criteria = nodeBuilder.createTupleQuery();
final Root<BasicEntity> root = criteria.from( BasicEntity.class );
final EntityType<BasicEntity> model = root.getModel();
criteria.select(
nodeBuilder.tuple(
root.get( model.getDeclaredSingularAttribute( "id", Integer.class ) ),
root.get( model.getDeclaredSingularAttribute( "data", String.class ) )
)
);
final List<Tuple> results = session.createQuery( criteria ).list();
assertThat( results ).hasSize( 1 );
final Tuple firstResult = results.get( 0 );
assertThat( firstResult.get(0) ).isEqualTo( 1 );
assertThat( firstResult.get(1) ).isEqualTo( "abc" );
} );
}
@Test
public void typedTupleTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
final CriteriaQuery<Tuple> criteria = nodeBuilder.createQuery( Tuple.class );
final Root<BasicEntity> root = criteria.from( BasicEntity.class );
final EntityType<BasicEntity> model = root.getModel();
criteria.select(
nodeBuilder.tuple(
root.get( model.getDeclaredSingularAttribute( "id", Integer.class ) ),
root.get( model.getDeclaredSingularAttribute( "data", String.class ) )
)
);
final List<Tuple> results = session.createQuery( criteria ).list();
assertThat( results ).hasSize( 1 );
final Tuple firstResult = results.get( 0 );
assertThat( firstResult.get(0) ).isEqualTo( 1 );
assertThat( firstResult.get(1) ).isEqualTo( "abc" );
} );
}
@Test
public void multiSelectTupleTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
final CriteriaQuery<Tuple> criteria = nodeBuilder.createTupleQuery();
final Root<BasicEntity> root = criteria.from( BasicEntity.class );
final EntityType<BasicEntity> model = root.getModel();
criteria.multiselect(
root.get( model.getDeclaredSingularAttribute( "id", Integer.class ) ),
root.get( model.getDeclaredSingularAttribute( "data", String.class ) )
);
final List<Tuple> results = session.createQuery( criteria ).list();
assertThat( results ).hasSize( 1 );
final Tuple firstResult = results.get( 0 );
assertThat( firstResult.get(0) ).isEqualTo( 1 );
assertThat( firstResult.get(1) ).isEqualTo( "abc" );
} );
}
@Test
public void arrayTupleTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
final CriteriaQuery<Tuple> criteria = nodeBuilder.createTupleQuery();
final Root<BasicEntity> root = criteria.from( BasicEntity.class );
final EntityType<BasicEntity> model = root.getModel();
criteria.select(
(Selection) nodeBuilder.array(
root.get( model.getDeclaredSingularAttribute( "id", Integer.class ) ),
root.get( model.getDeclaredSingularAttribute( "data", String.class ) )
)
);
final List<Tuple> results = session.createQuery( criteria ).list();
assertThat( results ).hasSize( 1 );
final Tuple firstResult = results.get( 0 );
assertThat( firstResult.get(0) ).isEqualTo( 1 );
assertThat( firstResult.get(1) ).isEqualTo( "abc" );
} );
}
@Test
public void singleSelectionTupleTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
final CriteriaQuery criteria = nodeBuilder.createTupleQuery();
final Root<BasicEntity> root = criteria.from( BasicEntity.class );
final EntityType<BasicEntity> model = root.getModel();
criteria.select(
root.get( model.getDeclaredSingularAttribute( "id", Integer.class ) )
);
final List<Tuple> results = session.createQuery( criteria ).list();
assertThat( results ).hasSize( 1 );
final Tuple firstResult = results.get( 0 );
assertThat( firstResult.get(0) ).isEqualTo( 1 );
} );
}
@BeforeEach
public void createTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.persist( new BasicEntity( 1, "abc" ) ) );
}
@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createQuery( "delete BasicEntity" ).executeUpdate() );
}
}