Skip to content

HHH-19712 Column deduplication leads to wrong alias calculation for native query alias expansion #10761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1876,62 +1876,62 @@ public String selectFragment(String alias, String suffix) {
// Wrap expressions with aliases
final SelectClause selectClause = rootQuerySpec.getSelectClause();
final List<SqlSelection> sqlSelections = selectClause.getSqlSelections();
final Set<String> processedExpressions = new HashSet<>( sqlSelections.size() );
int i = 0;
int columnIndex = 0;
final String[] columnAliases = getSubclassColumnAliasClosure();
final int columnAliasesSize = columnAliases.length;
for ( String identifierAlias : identifierAliases ) {
sqlSelections.set(
i,
new SqlSelectionImpl(
i,
new AliasedExpression( sqlSelections.get( i ).getExpression(), identifierAlias + suffix )
)
);
if ( i < columnAliasesSize && columnAliases[i].equals( identifierAlias ) ) {
columnIndex++;
final int identifierSelectionSize = identifierMapping.getJdbcTypeCount();
for ( int j = 0; j < identifierSelectionSize; j++ ) {
final SelectableMapping selectableMapping = identifierMapping.getSelectable( j );
if ( processedExpressions.add( selectableMapping.getSelectionExpression() ) ) {
aliasSelection( sqlSelections, i, identifierAliases[j] + suffix );
i++;
}
i++;
}

if ( entityMetamodel.hasSubclasses() ) {
sqlSelections.set(
i,
new SqlSelectionImpl(
i,
new AliasedExpression( sqlSelections.get( i ).getExpression(), getDiscriminatorAlias() + suffix )
)
);
i++;
if ( hasSubclasses() ) {
assert discriminatorMapping.getJdbcTypeCount() == 1;
final SelectableMapping selectableMapping = discriminatorMapping.getSelectable( 0 );
if ( processedExpressions.add( selectableMapping.getSelectionExpression() ) ) {
aliasSelection( sqlSelections, i, getDiscriminatorAlias() + suffix );
i++;
}
}

if ( hasRowId() ) {
sqlSelections.set(
i,
new SqlSelectionImpl(
i,
new AliasedExpression( sqlSelections.get( i ).getExpression(), ROWID_ALIAS + suffix )
)
);
i++;
final SelectableMapping selectableMapping = rowIdMapping;
if ( processedExpressions.add( selectableMapping.getSelectionExpression() ) ) {
aliasSelection( sqlSelections, i, ROWID_ALIAS + suffix );
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 );
final ColumnReference columnReference = (ColumnReference) sqlSelection.getExpression();
final String selectAlias =
columnReference.isColumnExpressionFormula()
? formulaAliases[formulaIndex++] + suffix
: columnAliases[columnIndex++] + suffix;
sqlSelections.set(
i,
new SqlSelectionImpl(
sqlSelection.getValuesArrayPosition(),
new AliasedExpression( sqlSelection.getExpression(), selectAlias )
)
);
final int size = getNumberOfFetchables();
// getSubclassColumnAliasClosure contains the _identifierMapper columns when it has an id class,
// which need to be skipped
if ( identifierMapping instanceof NonAggregatedIdentifierMapping nonAggregatedIdentifierMapping
&& nonAggregatedIdentifierMapping.getIdClassEmbeddable() != null ) {
columnIndex = identifierSelectionSize;
}
for ( int j = 0; j < size; j++ ) {
final AttributeMapping fetchable = getFetchable( j );
if ( !(fetchable instanceof PluralAttributeMapping)
&& !skipFetchable( fetchable, fetchable.getMappedFetchOptions().getTiming() )
&& fetchable.isSelectable() ) {
final int jdbcTypeCount = fetchable.getJdbcTypeCount();
for ( int k = 0; k < jdbcTypeCount; k++ ) {
final SelectableMapping selectableMapping = fetchable.getSelectable( k );
if ( processedExpressions.add( selectableMapping.getSelectionExpression() ) ) {
final String baseAlias = selectableMapping.isFormula()
? formulaAliases[formulaIndex++]
: columnAliases[columnIndex++];
aliasSelection( sqlSelections, i, baseAlias + suffix );
i++;
}
}
}
}

final String sql =
Expand All @@ -1945,6 +1945,17 @@ public String selectFragment(String alias, String suffix) {
: sql.substring( "select ".length() );
}

private static void aliasSelection(
List<SqlSelection> sqlSelections,
int selectionIndex,
String alias) {
final Expression expression = sqlSelections.get( selectionIndex ).getExpression();
sqlSelections.set(
selectionIndex,
new SqlSelectionImpl( selectionIndex, new AliasedExpression( expression, alias ) )
);
}

private ImmutableFetchList fetchProcessor(FetchParent fetchParent, LoaderSqlAstCreationState creationState) {
final FetchableContainer fetchableContainer = fetchParent.getReferencedMappingContainer();
final int size = fetchableContainer.getNumberOfFetchables();
Expand Down Expand Up @@ -5902,7 +5913,7 @@ public Fetchable getKeyFetchable(int position) {
}

@Override
public Fetchable getFetchable(int position) {
public AttributeMapping getFetchable(int position) {
return getStaticFetchableList().get( position );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.query.sql;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;

@DomainModel(annotatedClasses = {
NativeQueryResultBuilderColumnDeduplicationTest.MyEntity.class
})
@SessionFactory
@Jira("https://hibernate.atlassian.net/browse/HHH-19712")
public class NativeQueryResultBuilderColumnDeduplicationTest {

@Test
public void test(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createNativeQuery( "select {t.*} from MyEntity t", Object.class )
.addEntity( "t", MyEntity.class )
.getResultList();
}
);
}

@Entity(name = "MyEntity")
public static class MyEntity {
@EmbeddedId
private MyEntityPk id;
@Column(insertable = false, updatable = false)
private String name;
private String description;
}

@Embeddable
public static class MyEntityPk {
private String id;
private String name;
}
}