From 8ddc6e510545529ccf9dc01434d8800a62cb3842 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 8 Sep 2023 19:29:03 +0200 Subject: [PATCH] HHH-17108 Error In Native Query when adding two Entity's and using composite key. Error: Unable to find column position by name. --- .../persister/entity/AbstractEntityPersister.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index 80ae0abca9..1973d0474e 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -29,7 +29,6 @@ import java.util.TreeMap; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Supplier; -import java.util.stream.Collectors; import org.hibernate.AssertionFailure; import org.hibernate.FetchMode; @@ -1809,6 +1808,9 @@ public abstract class AbstractEntityPersister final SelectClause selectClause = rootQuerySpec.getSelectClause(); final List sqlSelections = selectClause.getSqlSelections(); int i = 0; + int columnIndex = 0; + final String[] columnAliases = getSubclassColumnAliasClosure(); + final int columnAliasesSize = columnAliases.length; for ( String identifierAlias : identifierAliases ) { sqlSelections.set( i, @@ -1817,6 +1819,9 @@ public abstract class AbstractEntityPersister new AliasedExpression( sqlSelections.get( i ).getExpression(), identifierAlias + suffix ) ) ); + if ( i < columnAliasesSize && columnAliases[i].equals( identifierAlias ) ) { + columnIndex++; + } i++; } @@ -1842,9 +1847,7 @@ public abstract class AbstractEntityPersister i++; } - final String[] columnAliases = getSubclassColumnAliasClosure(); final String[] formulaAliases = getSubclassFormulaAliasClosure(); - int columnIndex =0; int formulaIndex = 0; for ( ; i < sqlSelections.size(); i++ ) { final SqlSelection sqlSelection = sqlSelections.get( i );