@@ -1876,62 +1876,62 @@ 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+ // 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+ }
19351935 }
19361936
19371937 final String sql =
@@ -1945,6 +1945,17 @@ public String selectFragment(String alias, String suffix) {
19451945 : sql .substring ( "select " .length () );
19461946 }
19471947
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+
19481959 private ImmutableFetchList fetchProcessor (FetchParent fetchParent , LoaderSqlAstCreationState creationState ) {
19491960 final FetchableContainer fetchableContainer = fetchParent .getReferencedMappingContainer ();
19501961 final int size = fetchableContainer .getNumberOfFetchables ();
@@ -5902,7 +5913,7 @@ public Fetchable getKeyFetchable(int position) {
59025913 }
59035914
59045915 @ Override
5905- public Fetchable getFetchable (int position ) {
5916+ public AttributeMapping getFetchable (int position ) {
59065917 return getStaticFetchableList ().get ( position );
59075918 }
59085919
0 commit comments