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) {