HHH-17085 Improved check for entity-valued group by path expansion

This commit is contained in:
Marco Belladelli 2023-08-24 10:55:44 +02:00
parent 449f33ada1
commit 2105234f5f
1 changed files with 15 additions and 7 deletions

View File

@ -33,7 +33,6 @@ import org.hibernate.query.sqm.tree.domain.SqmPath;
import org.hibernate.query.sqm.tree.select.SqmDynamicInstantiation; import org.hibernate.query.sqm.tree.select.SqmDynamicInstantiation;
import org.hibernate.query.sqm.tree.select.SqmDynamicInstantiationArgument; import org.hibernate.query.sqm.tree.select.SqmDynamicInstantiationArgument;
import org.hibernate.query.sqm.tree.select.SqmQuerySpec; import org.hibernate.query.sqm.tree.select.SqmQuerySpec;
import org.hibernate.query.sqm.tree.select.SqmSelectableNode;
import org.hibernate.query.sqm.tree.select.SqmSelection; import org.hibernate.query.sqm.tree.select.SqmSelection;
import org.hibernate.spi.NavigablePath; import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.ast.Clause; import org.hibernate.sql.ast.Clause;
@ -50,6 +49,8 @@ import org.hibernate.sql.ast.tree.update.Assignable;
import org.hibernate.sql.results.graph.DomainResultCreationState; import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.Fetchable; import org.hibernate.sql.results.graph.Fetchable;
import jakarta.persistence.criteria.Selection;
public class EntityValuedPathInterpretation<T> extends AbstractSqmPathInterpretation<T> public class EntityValuedPathInterpretation<T> extends AbstractSqmPathInterpretation<T>
implements SqlTupleContainer, Assignable { implements SqlTupleContainer, Assignable {
private final Expression sqlExpression; private final Expression sqlExpression;
@ -361,20 +362,27 @@ public class EntityValuedPathInterpretation<T> extends AbstractSqmPathInterpreta
? Collections.emptyList() ? Collections.emptyList()
: sqmQuerySpec.getSelectClause().getSelections(); : sqmQuerySpec.getSelectClause().getSelections();
for ( SqmSelection<?> selection : selections ) { for ( SqmSelection<?> selection : selections ) {
if ( selectableNodeContains( selection.getSelectableNode(), path ) ) { if ( selectionContains( selection.getSelectableNode(), path ) ) {
return true; return true;
} }
} }
return false; return false;
} }
private static boolean selectableNodeContains(SqmSelectableNode<?> selectableNode, NavigablePath path) { private static boolean selectionContains(Selection<?> selection, NavigablePath path) {
if ( selectableNode instanceof SqmPath && path.isParentOrEqual( ( (SqmPath<?>) selectableNode ).getNavigablePath() ) ) { if ( selection instanceof SqmPath && path.isParentOrEqual( ( (SqmPath<?>) selection ).getNavigablePath() ) ) {
return true; return true;
} }
else if ( selectableNode instanceof SqmDynamicInstantiation ) { else if ( selection.isCompoundSelection() ) {
for ( SqmDynamicInstantiationArgument<?> argument : ( (SqmDynamicInstantiation<?>) selectableNode ).getArguments() ) { for ( Selection<?> compoundSelection : selection.getCompoundSelectionItems() ) {
if ( selectableNodeContains( argument.getSelectableNode(), path ) ) { if ( selectionContains( compoundSelection, path ) ) {
return true;
}
}
}
else if ( selection instanceof SqmDynamicInstantiation ) {
for ( SqmDynamicInstantiationArgument<?> argument : ( (SqmDynamicInstantiation<?>) selection ).getArguments() ) {
if ( selectionContains( argument.getSelectableNode(), path ) ) {
return true; return true;
} }
} }