From 504dd2c6b404b3ad4b3256bd493ab7ad9f9e3e03 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 20 Sep 2021 17:48:48 +0200 Subject: [PATCH] Fix AliasToMap converter for JPA Criteria --- .../internal/ConcreteSqmSelectQueryPlan.java | 21 ++++++++++++------- .../tree/select/SqmJpaCompoundSelection.java | 8 +++++++ .../sqm/tree/select/SqmSelectableNode.java | 4 ++++ .../query/sqm/tree/select/SqmSelection.java | 4 ++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/ConcreteSqmSelectQueryPlan.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/ConcreteSqmSelectQueryPlan.java index df49538864..bb39eafd79 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/ConcreteSqmSelectQueryPlan.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/ConcreteSqmSelectQueryPlan.java @@ -6,6 +6,7 @@ */ package org.hibernate.query.sqm.internal; +import java.util.ArrayList; import java.util.Collections; import java.util.IdentityHashMap; import java.util.List; @@ -20,7 +21,6 @@ import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.internal.EmptyScrollableResults; -import org.hibernate.internal.util.streams.StingArrayCollector; import org.hibernate.metamodel.mapping.MappingModelExpressable; import org.hibernate.query.IllegalQueryOperationException; import org.hibernate.query.criteria.JpaSelection; @@ -34,7 +34,6 @@ import org.hibernate.query.sqm.sql.SqmTranslation; import org.hibernate.query.sqm.sql.SqmTranslator; import org.hibernate.query.sqm.sql.SqmTranslatorFactory; import org.hibernate.query.sqm.tree.expression.SqmParameter; -import org.hibernate.query.sqm.tree.select.SqmJpaCompoundSelection; import org.hibernate.query.sqm.tree.select.SqmSelectStatement; import org.hibernate.query.sqm.tree.select.SqmSelection; import org.hibernate.sql.ast.SqlAstTranslator; @@ -188,12 +187,20 @@ public class ConcreteSqmSelectQueryPlan implements SelectQueryPlan { private RowTransformer makeRowTransformerTupleTransformerAdapter( SqmSelectStatement sqm, QueryOptions queryOptions) { + final List aliases = new ArrayList<>(); + sqm.getQuerySpec().getSelectClause().getSelections() + .stream() + .forEach( + sqmSelection -> { + final String[] selectionAliases = sqmSelection.getAliases(); + for ( int i = 0; i < selectionAliases.length; i++ ) { + final String alias = selectionAliases[i]; + aliases.add( alias ); + } + } + ); return new RowTransformerTupleTransformerAdapter<>( - sqm.getQuerySpec().getSelectClause().getSelections() - .stream() - .map( SqmSelection::getAlias ) - .collect( StingArrayCollector.INSTANCE ), - queryOptions.getTupleTransformer() + aliases.toArray( new String[] {} ), queryOptions.getTupleTransformer() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmJpaCompoundSelection.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmJpaCompoundSelection.java index 61dc402e04..ed7eed700b 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmJpaCompoundSelection.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmJpaCompoundSelection.java @@ -99,6 +99,14 @@ public class SqmJpaCompoundSelection return null; } + public String[] getAliases() { + String[] aliases = new String[selectableNodes.size()]; + for ( int i = 0; i < selectableNodes.size(); i++ ) { + aliases[i] = selectableNodes.get( i ).getAlias(); + } + return aliases; + } + @Override public boolean isCompoundSelection() { return true; diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmSelectableNode.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmSelectableNode.java index 3b01a971ed..e8a708db00 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmSelectableNode.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmSelectableNode.java @@ -29,4 +29,8 @@ public interface SqmSelectableNode extends JpaSelection, SqmTypedNode, * @see Selection#getCompoundSelectionItems() */ void visitSubSelectableNodes(Consumer> jpaSelectionConsumer); + + default String[] getAliases() { + return new String[] { getAlias() }; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmSelection.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmSelection.java index e03652f896..b5b6cff806 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmSelection.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmSelection.java @@ -50,6 +50,10 @@ public class SqmSelection extends AbstractSqmNode implements SqmAliasedNode X accept(SemanticQueryWalker walker) { return walker.visitSelection( this );