Don't change result type of criteria query when changing select clause

This commit is contained in:
Christian Beikov 2024-02-22 13:34:50 +01:00
parent 12a93ce328
commit 94cb793180
4 changed files with 25 additions and 27 deletions

View File

@ -225,7 +225,7 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
if ( queryOptions.getTupleTransformer() != null ) {
return makeRowTransformerTupleTransformerAdapter( sqm, queryOptions );
}
else if ( resultClass == null ) {
else if ( resultClass == null || resultClass == Object.class ) {
return RowTransformerStandardImpl.instance();
}
else {

View File

@ -47,7 +47,7 @@ public abstract class AbstractSqmSelectQuery<T>
implements SqmSelectQuery<T> {
private final Map<String, SqmCteStatement<?>> cteStatements;
private SqmQueryPart<T> sqmQueryPart;
private Class<T> resultType;
private final Class<T> resultType;
public AbstractSqmSelectQuery(Class<T> resultType, NodeBuilder builder) {
this( new SqmQuerySpec<>( builder ), resultType, builder );
@ -204,8 +204,12 @@ public abstract class AbstractSqmSelectQuery<T>
return resultType;
}
/**
* @deprecated Don't use this method. It has no effect.
*/
@Deprecated(forRemoval = true)
protected void setResultType(Class<T> resultType) {
this.resultType = resultType;
// No-op
}
@Override
@ -412,7 +416,6 @@ public abstract class AbstractSqmSelectQuery<T>
break;
}
default: {
setResultType( (Class<T>) Object[].class );
resultSelection = ( Selection<? extends T> ) nodeBuilder().array( selections );
}
}

View File

@ -119,6 +119,10 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
if ( existing != null ) {
return existing;
}
return createCopy( context, getResultType() );
}
private <X> SqmSelectStatement<X> createCopy(SqmCopyContext context, Class<X> resultType) {
final Set<SqmParameter<?>> parameters;
if ( this.parameters == null ) {
parameters = null;
@ -129,17 +133,19 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
parameters.add( parameter.copy( context ) );
}
}
final SqmSelectStatement<T> statement = context.registerCopy(
//noinspection unchecked
final SqmSelectStatement<X> statement = (SqmSelectStatement<X>) context.registerCopy(
this,
new SqmSelectStatement<>(
nodeBuilder(),
copyCteStatements( context ),
getResultType(),
resultType,
getQuerySource(),
parameters
)
);
statement.setQueryPart( getQueryPart().copy( context ) );
//noinspection unchecked
statement.setQueryPart( (SqmQueryPart<X>) getQueryPart().copy( context ) );
return statement;
}
@ -266,9 +272,6 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
checkSelectionIsJpaCompliant( selection );
}
getQuerySpec().setSelection( (JpaSelection<T>) selection );
if ( getResultType() == Object.class ) {
setResultType( (Class<T>) selection.getJavaType() );
}
return this;
}
@ -313,7 +316,6 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
return (Selection<? extends T>) selectionList.get( 0 );
}
default: {
setResultType( (Class<T>) Object[].class );
return (Selection<? extends T>) nodeBuilder().array( selections );
}
}
@ -461,14 +463,14 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
@Override
public SqmSelectStatement<Long> createCountQuery() {
final SqmSelectStatement<?> copy = copy( noParamCopyContext() );
final SqmQuerySpec<?> querySpec = copy.getQuerySpec();
final SqmSelectStatement<?> copy = createCopy( noParamCopyContext(), Object.class );
final SqmQueryPart<?> queryPart = copy.getQueryPart();
final SqmQuerySpec<?> querySpec;
//TODO: detect queries with no 'group by', but aggregate functions
// in 'select' list (we don't even need to hit the database to
// know they return exactly one row)
if ( queryPart.isSimpleQueryPart()
&& !querySpec.isDistinct()
&& !( querySpec = (SqmQuerySpec<?>) queryPart ).isDistinct()
&& querySpec.getGroupingExpressions().isEmpty() ) {
for ( SqmRoot<?> root : querySpec.getRootList() ) {
root.removeLeftFetchJoins();
@ -478,24 +480,21 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
querySpec.setOrderByClause( null );
}
@SuppressWarnings("unchecked")
final SqmSelectStatement<Long> statement = (SqmSelectStatement<Long>) copy;
statement.setResultType( Long.class );
return statement;
return (SqmSelectStatement<Long>) copy;
}
else {
final JpaSelection<?> selection = querySpec.getSelection();
final JpaSelection<?> selection = queryPart.getFirstQuerySpec().getSelection();
if ( selection.isCompoundSelection() ) {
char c = 'a';
for ( JpaSelection<?> item: selection.getSelectionItems() ) {
item.alias( Character.toString(++c) + '_' );
for ( JpaSelection<?> item : selection.getSelectionItems() ) {
item.alias( Character.toString( ++c ) + '_' );
}
}
else {
selection.alias("a_");
selection.alias( "a_" );
}
final SqmSubQuery<?> subquery = new SqmSubQuery<>( copy, queryPart, null, nodeBuilder() );
final SqmSelectStatement<Long> query = nodeBuilder().createQuery(Long.class);
final SqmSelectStatement<Long> query = nodeBuilder().createQuery( Long.class );
query.from( subquery );
query.select( nodeBuilder().count() );
return query;

View File

@ -258,7 +258,6 @@ public class SqmSubQuery<T> extends AbstractSqmSelectQuery<T> implements SqmSele
break;
}
default: {
setResultType( (Class<T>) Object[].class );
resultSelection = ( Selection<? extends T> ) nodeBuilder().array( selections );
}
}
@ -609,9 +608,6 @@ public class SqmSubQuery<T> extends AbstractSqmSelectQuery<T> implements SqmSele
public void applyInferableType(SqmExpressible<?> type) {
//noinspection unchecked
expressibleType = (SqmExpressible<T>) type;
if ( expressibleType != null && expressibleType.getExpressibleJavaType() != null ) {
setResultType( expressibleType.getExpressibleJavaType().getJavaTypeClass() );
}
}
private void applyInferableType(Class<T> type) {