From 0745a2e294af5d4d796e997cfe7ef1c1d85b12bf Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 5 Dec 2022 16:03:26 +0100 Subject: [PATCH] HHH-15749 Hibernate fails when selecting grouped by entities in a specific order --- .../query/sqm/sql/BaseSqmToSqlAstConverter.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java index 2278b1a9d7..d163d97e88 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java @@ -2288,11 +2288,11 @@ public abstract class BaseSqmToSqlAstConverter extends Base private int indexOfExpression(List> selections, SqmExpression node) { final int result = indexOfExpression( 0, selections, node ); - if ( result < 1 ) { - return -result; + if ( result < 0 ) { + return -1; } else { - return -1; + return result; } } @@ -2308,27 +2308,27 @@ public abstract class BaseSqmToSqlAstConverter extends Base ( (SqmDynamicInstantiation) selectableNode ).getArguments(), node ); - if ( subResult < 0 ) { + if ( subResult >= 0 ) { return subResult; } - offset = subResult - i; + offset = -subResult - i; } else if ( selectableNode instanceof SqmJpaCompoundSelection ) { final List> selectionItems = ( (SqmJpaCompoundSelection) selectableNode ).getSelectionItems(); for ( int j = 0; j < selectionItems.size(); j++ ) { if ( selectionItems.get( j ) == node ) { - return -( offset + i + j ); + return offset + i + j; } } offset += selectionItems.size(); } else { if ( selectableNode == node ) { - return -( offset + i ); + return offset + i; } } } - return offset + selections.size(); + return -( offset + selections.size() ); } private boolean selectClauseContains(SqmFrom from) {