@@ -1876,62 +1876,56 @@ 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+ int columnIndex = 0 ;
19201910 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+ for ( int j = 0 ; j < size ; j ++ ) {
1913+ final AttributeMapping fetchable = getFetchable ( j );
1914+ if ( !(fetchable instanceof PluralAttributeMapping )
1915+ && !skipFetchable ( fetchable , fetchable .getMappedFetchOptions ().getTiming () )
1916+ && fetchable .isSelectable () ) {
1917+ final int jdbcTypeCount = fetchable .getJdbcTypeCount ();
1918+ for ( int k = 0 ; k < jdbcTypeCount ; k ++ ) {
1919+ final SelectableMapping selectableMapping = fetchable .getSelectable ( k );
1920+ final String baseAlias = selectableMapping .isFormula ()
1921+ ? formulaAliases [formulaIndex ++]
1922+ : columnAliases [columnIndex ++];
1923+ if ( processedExpressions .add ( selectableMapping .getSelectionExpression () ) ) {
1924+ aliasSelection ( sqlSelections , i , baseAlias + suffix );
1925+ i ++;
1926+ }
1927+ }
1928+ }
19351929 }
19361930
19371931 final String sql =
@@ -1945,6 +1939,17 @@ public String selectFragment(String alias, String suffix) {
19451939 : sql .substring ( "select " .length () );
19461940 }
19471941
1942+ private static void aliasSelection (
1943+ List <SqlSelection > sqlSelections ,
1944+ int selectionIndex ,
1945+ String alias ) {
1946+ final Expression expression = sqlSelections .get ( selectionIndex ).getExpression ();
1947+ sqlSelections .set (
1948+ selectionIndex ,
1949+ new SqlSelectionImpl ( selectionIndex , new AliasedExpression ( expression , alias ) )
1950+ );
1951+ }
1952+
19481953 private ImmutableFetchList fetchProcessor (FetchParent fetchParent , LoaderSqlAstCreationState creationState ) {
19491954 final FetchableContainer fetchableContainer = fetchParent .getReferencedMappingContainer ();
19501955 final int size = fetchableContainer .getNumberOfFetchables ();
@@ -5902,7 +5907,7 @@ public Fetchable getKeyFetchable(int position) {
59025907 }
59035908
59045909 @ Override
5905- public Fetchable getFetchable (int position ) {
5910+ public AttributeMapping getFetchable (int position ) {
59065911 return getStaticFetchableList ().get ( position );
59075912 }
59085913
0 commit comments