@@ -1876,62 +1876,62 @@ public String selectFragment(String alias, String suffix) {
1876
1876
// Wrap expressions with aliases
1877
1877
final SelectClause selectClause = rootQuerySpec .getSelectClause ();
1878
1878
final List <SqlSelection > sqlSelections = selectClause .getSqlSelections ();
1879
+ final Set <String > processedExpressions = new HashSet <>( sqlSelections .size () );
1879
1880
int i = 0 ;
1880
- int columnIndex = 0 ;
1881
- final String [] columnAliases = getSubclassColumnAliasClosure ();
1882
- final int columnAliasesSize = columnAliases .length ;
1883
- for ( String identifierAlias : identifierAliases ) {
1884
- sqlSelections .set (
1885
- i ,
1886
- new SqlSelectionImpl (
1887
- i ,
1888
- new AliasedExpression ( sqlSelections .get ( i ).getExpression (), identifierAlias + suffix )
1889
- )
1890
- );
1891
- if ( i < columnAliasesSize && columnAliases [i ].equals ( identifierAlias ) ) {
1892
- columnIndex ++;
1881
+ final int identifierSelectionSize = identifierMapping .getJdbcTypeCount ();
1882
+ for ( int j = 0 ; j < identifierSelectionSize ; j ++ ) {
1883
+ final SelectableMapping selectableMapping = identifierMapping .getSelectable ( j );
1884
+ if ( processedExpressions .add ( selectableMapping .getSelectionExpression () ) ) {
1885
+ aliasSelection ( sqlSelections , i , identifierAliases [j ] + suffix );
1886
+ i ++;
1893
1887
}
1894
- i ++;
1895
1888
}
1896
1889
1897
- if ( entityMetamodel .hasSubclasses () ) {
1898
- sqlSelections .set (
1899
- i ,
1900
- new SqlSelectionImpl (
1901
- i ,
1902
- new AliasedExpression ( sqlSelections .get ( i ).getExpression (), getDiscriminatorAlias () + suffix )
1903
- )
1904
- );
1905
- i ++;
1890
+ if ( hasSubclasses () ) {
1891
+ assert discriminatorMapping .getJdbcTypeCount () == 1 ;
1892
+ final SelectableMapping selectableMapping = discriminatorMapping .getSelectable ( 0 );
1893
+ if ( processedExpressions .add ( selectableMapping .getSelectionExpression () ) ) {
1894
+ aliasSelection ( sqlSelections , i , getDiscriminatorAlias () + suffix );
1895
+ i ++;
1896
+ }
1906
1897
}
1907
1898
1908
1899
if ( hasRowId () ) {
1909
- sqlSelections .set (
1910
- i ,
1911
- new SqlSelectionImpl (
1912
- i ,
1913
- new AliasedExpression ( sqlSelections .get ( i ).getExpression (), ROWID_ALIAS + suffix )
1914
- )
1915
- );
1916
- i ++;
1900
+ final SelectableMapping selectableMapping = rowIdMapping ;
1901
+ if ( processedExpressions .add ( selectableMapping .getSelectionExpression () ) ) {
1902
+ aliasSelection ( sqlSelections , i , ROWID_ALIAS + suffix );
1903
+ i ++;
1904
+ }
1917
1905
}
1918
1906
1907
+ final String [] columnAliases = getSubclassColumnAliasClosure ();
1919
1908
final String [] formulaAliases = getSubclassFormulaAliasClosure ();
1909
+ int columnIndex = 0 ;
1920
1910
int formulaIndex = 0 ;
1921
- for ( ; i < sqlSelections .size (); i ++ ) {
1922
- final SqlSelection sqlSelection = sqlSelections .get ( i );
1923
- final ColumnReference columnReference = (ColumnReference ) sqlSelection .getExpression ();
1924
- final String selectAlias =
1925
- columnReference .isColumnExpressionFormula ()
1926
- ? formulaAliases [formulaIndex ++] + suffix
1927
- : columnAliases [columnIndex ++] + suffix ;
1928
- sqlSelections .set (
1929
- i ,
1930
- new SqlSelectionImpl (
1931
- sqlSelection .getValuesArrayPosition (),
1932
- new AliasedExpression ( sqlSelection .getExpression (), selectAlias )
1933
- )
1934
- );
1911
+ final int size = getNumberOfFetchables ();
1912
+ // getSubclassColumnAliasClosure contains the _identifierMapper columns when it has an id class,
1913
+ // which need to be skipped
1914
+ if ( identifierMapping instanceof NonAggregatedIdentifierMapping nonAggregatedIdentifierMapping
1915
+ && nonAggregatedIdentifierMapping .getIdClassEmbeddable () != null ) {
1916
+ columnIndex = identifierSelectionSize ;
1917
+ }
1918
+ for ( int j = 0 ; j < size ; j ++ ) {
1919
+ final AttributeMapping fetchable = getFetchable ( j );
1920
+ if ( !(fetchable instanceof PluralAttributeMapping )
1921
+ && !skipFetchable ( fetchable , fetchable .getMappedFetchOptions ().getTiming () )
1922
+ && fetchable .isSelectable () ) {
1923
+ final int jdbcTypeCount = fetchable .getJdbcTypeCount ();
1924
+ for ( int k = 0 ; k < jdbcTypeCount ; k ++ ) {
1925
+ final SelectableMapping selectableMapping = fetchable .getSelectable ( k );
1926
+ if ( processedExpressions .add ( selectableMapping .getSelectionExpression () ) ) {
1927
+ final String baseAlias = selectableMapping .isFormula ()
1928
+ ? formulaAliases [formulaIndex ++]
1929
+ : columnAliases [columnIndex ++];
1930
+ aliasSelection ( sqlSelections , i , baseAlias + suffix );
1931
+ i ++;
1932
+ }
1933
+ }
1934
+ }
1935
1935
}
1936
1936
1937
1937
final String sql =
@@ -1945,6 +1945,17 @@ public String selectFragment(String alias, String suffix) {
1945
1945
: sql .substring ( "select " .length () );
1946
1946
}
1947
1947
1948
+ private static void aliasSelection (
1949
+ List <SqlSelection > sqlSelections ,
1950
+ int selectionIndex ,
1951
+ String alias ) {
1952
+ final Expression expression = sqlSelections .get ( selectionIndex ).getExpression ();
1953
+ sqlSelections .set (
1954
+ selectionIndex ,
1955
+ new SqlSelectionImpl ( selectionIndex , new AliasedExpression ( expression , alias ) )
1956
+ );
1957
+ }
1958
+
1948
1959
private ImmutableFetchList fetchProcessor (FetchParent fetchParent , LoaderSqlAstCreationState creationState ) {
1949
1960
final FetchableContainer fetchableContainer = fetchParent .getReferencedMappingContainer ();
1950
1961
final int size = fetchableContainer .getNumberOfFetchables ();
@@ -5902,7 +5913,7 @@ public Fetchable getKeyFetchable(int position) {
5902
5913
}
5903
5914
5904
5915
@ Override
5905
- public Fetchable getFetchable (int position ) {
5916
+ public AttributeMapping getFetchable (int position ) {
5906
5917
return getStaticFetchableList ().get ( position );
5907
5918
}
5908
5919
0 commit comments