HHH-13140 Criteria API multiselect aliases are not passed through to the JPQL query and they are not available in ResultTransformer

This commit is contained in:
Andrea Boriero 2021-09-20 20:24:54 +02:00
parent 6cb13ddf0c
commit ca83c6ca7e
8 changed files with 23 additions and 42 deletions

View File

@ -21,6 +21,7 @@ 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.collections.ArrayHelper;
import org.hibernate.metamodel.mapping.MappingModelExpressable;
import org.hibernate.query.IllegalQueryOperationException;
import org.hibernate.query.criteria.JpaSelection;
@ -188,19 +189,15 @@ public class ConcreteSqmSelectQueryPlan<R> implements SelectQueryPlan<R> {
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 );
}
}
);
sqm.getQuerySpec().getSelectClause().getSelections().forEach(
sqmSelection ->
sqmSelection.getSelectableNode().visitSubSelectableNodes(
subSelection -> aliases.add( subSelection.getAlias() )
)
);
return new RowTransformerTupleTransformerAdapter<>(
aliases.toArray( new String[] {} ), queryOptions.getTupleTransformer()
ArrayHelper.toStringArray( aliases ), queryOptions.getTupleTransformer()
);
}

View File

@ -33,6 +33,7 @@ import org.hibernate.metamodel.model.domain.SetPersistentAttribute;
import org.hibernate.metamodel.model.domain.SingularPersistentAttribute;
import org.hibernate.query.NavigablePath;
import org.hibernate.query.criteria.JpaPath;
import org.hibernate.query.criteria.JpaSelection;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.SemanticException;
import org.hibernate.query.sqm.SqmPathSource;
@ -608,4 +609,12 @@ public abstract class AbstractSqmFrom<O,T> extends AbstractSqmPath<T> implements
sb.append( alias );
}
}
@Override
public JpaSelection<T> alias(String name) {
if ( getExplicitAlias() == null ) {
setExplicitAlias( name );
}
return super.alias( name );
}
}

View File

@ -105,9 +105,4 @@ public class SqmSetJoin<O, E>
nodeBuilder()
);
}
@Override
public String getAlias() {
return getExplicitAlias();
}
}

View File

@ -84,9 +84,4 @@ public class SqmSingularJoin<O,T> extends AbstractSqmAttributeJoin<O,T> implemen
nodeBuilder()
);
}
@Override
public String getAlias() {
return getExplicitAlias();
}
}

View File

@ -7,6 +7,7 @@
package org.hibernate.query.sqm.tree.select;
import java.util.List;
import java.util.function.Consumer;
import javax.persistence.criteria.Selection;
@ -99,14 +100,6 @@ 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;
@ -124,7 +117,10 @@ public class SqmJpaCompoundSelection<T>
sb.append(", ");
selectableNodes.get( i ).appendHqlString( sb );
}
}
@Override
public void visitSubSelectableNodes(Consumer<SqmSelectableNode<?>> jpaSelectionConsumer) {
selectableNodes.forEach( jpaSelectionConsumer );
}
}

View File

@ -29,8 +29,4 @@ 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,10 +50,6 @@ 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 );

View File

@ -19,7 +19,6 @@ import javax.persistence.criteria.Root;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.query.Query;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.hibernate.transform.Transformers;
import org.junit.Before;
@ -49,7 +48,6 @@ public class CriteriaMultiselectAliasTest extends BaseEntityManagerFunctionalTes
}
@Test
@FailureExpected(jiraKey = "HHH-13140")
@TestForIssue(jiraKey = "HHH-13140")
public void testAlias() {
doInJPA( this::entityManagerFactory, entityManager -> {
@ -76,7 +74,6 @@ public class CriteriaMultiselectAliasTest extends BaseEntityManagerFunctionalTes
}
@Test
@FailureExpected(jiraKey = "HHH-13140")
@TestForIssue(jiraKey = "HHH-13192")
public void testNoAliasInWhereClause() {
doInJPA( this::entityManagerFactory, entityManager -> {