@@ -1876,62 +1876,58 @@ public String selectFragment(String alias, String suffix) {
18761876 // Wrap expressions with aliases
18771877 final SelectClause selectClause = rootQuerySpec .getSelectClause ();
18781878 final List <SqlSelection > sqlSelections = selectClause .getSqlSelections ();
1879+ final Set <String > processedExpressions = new HashSet <>( sqlSelections .size () );
18791880 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 ++;
18931887 }
1894- i ++;
18951888 }
18961889
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+ }
19061897 }
19071898
19081899 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+ }
19171905 }
19181906
1907+ final String [] columnAliases = getSubclassColumnAliasClosure ();
19191908 final String [] formulaAliases = getSubclassFormulaAliasClosure ();
1909+ // getSubclassColumnAliasClosure contains the _identifierMapper columns,
1910+ // which need to be skipped
1911+ int columnIndex = identifierMapping instanceof NonAggregatedIdentifierMapping ? identifierSelectionSize : 0 ;
19201912 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- );
1913+ final int size = getNumberOfFetchables ();
1914+ for ( int j = 0 ; j < size ; j ++ ) {
1915+ final AttributeMapping fetchable = getFetchable ( j );
1916+ if ( !(fetchable instanceof PluralAttributeMapping )
1917+ && !skipFetchable ( fetchable , fetchable .getMappedFetchOptions ().getTiming () )
1918+ && fetchable .isSelectable () ) {
1919+ final int jdbcTypeCount = fetchable .getJdbcTypeCount ();
1920+ for ( int k = 0 ; k < jdbcTypeCount ; k ++ ) {
1921+ final SelectableMapping selectableMapping = fetchable .getSelectable ( k );
1922+ if ( processedExpressions .add ( selectableMapping .getSelectionExpression () ) ) {
1923+ final String baseAlias = selectableMapping .isFormula ()
1924+ ? formulaAliases [formulaIndex ++]
1925+ : columnAliases [columnIndex ++];
1926+ aliasSelection ( sqlSelections , i , baseAlias + suffix );
1927+ i ++;
1928+ }
1929+ }
1930+ }
19351931 }
19361932
19371933 final String sql =
@@ -1945,6 +1941,17 @@ public String selectFragment(String alias, String suffix) {
19451941 : sql .substring ( "select " .length () );
19461942 }
19471943
1944+ private static void aliasSelection (
1945+ List <SqlSelection > sqlSelections ,
1946+ int selectionIndex ,
1947+ String alias ) {
1948+ final Expression expression = sqlSelections .get ( selectionIndex ).getExpression ();
1949+ sqlSelections .set (
1950+ selectionIndex ,
1951+ new SqlSelectionImpl ( selectionIndex , new AliasedExpression ( expression , alias ) )
1952+ );
1953+ }
1954+
19481955 private ImmutableFetchList fetchProcessor (FetchParent fetchParent , LoaderSqlAstCreationState creationState ) {
19491956 final FetchableContainer fetchableContainer = fetchParent .getReferencedMappingContainer ();
19501957 final int size = fetchableContainer .getNumberOfFetchables ();
@@ -5902,7 +5909,7 @@ public Fetchable getKeyFetchable(int position) {
59025909 }
59035910
59045911 @ Override
5905- public Fetchable getFetchable (int position ) {
5912+ public AttributeMapping getFetchable (int position ) {
59065913 return getStaticFetchableList ().get ( position );
59075914 }
59085915
0 commit comments