Fix AliasToMap converter for JPA Criteria

This commit is contained in:
Andrea Boriero 2021-09-20 17:48:48 +02:00
parent fcfb2074f2
commit 504dd2c6b4
4 changed files with 30 additions and 7 deletions

View File

@ -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<R> implements SelectQueryPlan<R> {
private RowTransformer<R> makeRowTransformerTupleTransformerAdapter(
SqmSelectStatement sqm,
QueryOptions queryOptions) {
final List<String> 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()
);
}

View File

@ -99,6 +99,14 @@ public class SqmJpaCompoundSelection<T>
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;

View File

@ -29,4 +29,8 @@ public interface SqmSelectableNode<T> extends JpaSelection<T>, SqmTypedNode<T>,
* @see Selection#getCompoundSelectionItems()
*/
void visitSubSelectableNodes(Consumer<SqmSelectableNode<?>> jpaSelectionConsumer);
default String[] getAliases() {
return new String[] { getAlias() };
}
}

View File

@ -50,6 +50,10 @@ public class SqmSelection<T> extends AbstractSqmNode implements SqmAliasedNode<T
return selectableNode.getAlias();
}
public String[] getAliases() {
return selectableNode.getAliases();
}
@Override
public <X> X accept(SemanticQueryWalker<X> walker) {
return walker.visitSelection( this );