HHH-16202 @OrderBy not applied to collections when @Fetch(value = FetchMode.SUBSELECT)

This commit is contained in:
Andrea Boriero 2023-02-20 13:46:38 +01:00 committed by Christian Beikov
parent 8776a8068a
commit 346da69979
1 changed files with 23 additions and 23 deletions

View File

@ -6,7 +6,6 @@
*/ */
package org.hibernate.loader.ast.internal; package org.hibernate.loader.ast.internal;
import java.util.AbstractMap;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -249,7 +248,6 @@ public class LoaderSelectBuilder {
private final EntityGraphTraversalState entityGraphTraversalState; private final EntityGraphTraversalState entityGraphTraversalState;
private int fetchDepth; private int fetchDepth;
private List<Map.Entry<OrderByFragment, TableGroup>> orderByFragments;
private boolean hasCollectionJoinFetches; private boolean hasCollectionJoinFetches;
private String currentBagRole; private String currentBagRole;
@ -474,22 +472,12 @@ public class LoaderSelectBuilder {
if ( loadable instanceof PluralAttributeMapping ) { if ( loadable instanceof PluralAttributeMapping ) {
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) loadable; final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) loadable;
applyFiltering( rootQuerySpec, rootTableGroup, pluralAttributeMapping, sqlAstCreationState ); applyFiltering( rootQuerySpec, rootTableGroup, pluralAttributeMapping, sqlAstCreationState );
applyOrdering( rootTableGroup, pluralAttributeMapping ); applyOrdering( rootQuerySpec, rootTableGroup, pluralAttributeMapping, sqlAstCreationState );
} }
else { else {
applyFiltering( rootQuerySpec, rootTableGroup, (Restrictable) loadable, sqlAstCreationState ); applyFiltering( rootQuerySpec, rootTableGroup, (Restrictable) loadable, sqlAstCreationState );
} }
if ( orderByFragments != null ) {
orderByFragments.forEach(
entry -> entry.getKey().apply(
rootQuerySpec,
entry.getValue(),
sqlAstCreationState
)
);
}
return new SelectStatement( rootQuerySpec, domainResults ); return new SelectStatement( rootQuerySpec, domainResults );
} }
@ -642,21 +630,31 @@ public class LoaderSelectBuilder {
); );
} }
private void applyOrdering(TableGroup tableGroup, PluralAttributeMapping pluralAttributeMapping) { private void applyOrdering(
QuerySpec querySpec,
TableGroup tableGroup,
PluralAttributeMapping pluralAttributeMapping,
SqlAstCreationState astCreationState) {
if ( pluralAttributeMapping.getOrderByFragment() != null ) { if ( pluralAttributeMapping.getOrderByFragment() != null ) {
applyOrdering( tableGroup, pluralAttributeMapping.getOrderByFragment() ); applyOrdering( querySpec, tableGroup, pluralAttributeMapping.getOrderByFragment(), astCreationState );
} }
if ( pluralAttributeMapping.getManyToManyOrderByFragment() != null ) { if ( pluralAttributeMapping.getManyToManyOrderByFragment() != null ) {
applyOrdering( tableGroup, pluralAttributeMapping.getManyToManyOrderByFragment() ); applyOrdering(
querySpec,
tableGroup,
pluralAttributeMapping.getManyToManyOrderByFragment(),
astCreationState
);
} }
} }
private void applyOrdering(TableGroup tableGroup, OrderByFragment orderByFragment) { private void applyOrdering(
if ( orderByFragments == null ) { QuerySpec querySpec,
orderByFragments = new ArrayList<>(); TableGroup tableGroup,
} OrderByFragment orderByFragment,
orderByFragments.add( new AbstractMap.SimpleEntry<>( orderByFragment, tableGroup ) ); SqlAstCreationState astCreationState) {
orderByFragment.apply( querySpec, tableGroup, astCreationState );
} }
private ImmutableFetchList visitFetches(FetchParent fetchParent, LoaderSqlAstCreationState creationState) { private ImmutableFetchList visitFetches(FetchParent fetchParent, LoaderSqlAstCreationState creationState) {
@ -859,6 +857,7 @@ public class LoaderSelectBuilder {
creationState creationState
); );
applyOrdering( applyOrdering(
querySpec,
fetchablePath, fetchablePath,
pluralAttributeMapping, pluralAttributeMapping,
creationState creationState
@ -906,6 +905,7 @@ public class LoaderSelectBuilder {
} }
private void applyOrdering( private void applyOrdering(
QuerySpec querySpec,
NavigablePath navigablePath, NavigablePath navigablePath,
PluralAttributeMapping pluralAttributeMapping, PluralAttributeMapping pluralAttributeMapping,
LoaderSqlAstCreationState sqlAstCreationState) { LoaderSqlAstCreationState sqlAstCreationState) {
@ -914,7 +914,7 @@ public class LoaderSelectBuilder {
final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().getTableGroup( navigablePath ); final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().getTableGroup( navigablePath );
assert tableGroup != null; assert tableGroup != null;
applyOrdering( tableGroup, pluralAttributeMapping ); applyOrdering( querySpec, tableGroup, pluralAttributeMapping, sqlAstCreationState );
} }
private SelectStatement generateSelect(SubselectFetch subselect) { private SelectStatement generateSelect(SubselectFetch subselect) {
@ -983,7 +983,7 @@ public class LoaderSelectBuilder {
// NOTE : no need to check - we are explicitly processing a plural-attribute // NOTE : no need to check - we are explicitly processing a plural-attribute
applyFiltering( rootQuerySpec, rootTableGroup, attributeMapping, sqlAstCreationState ); applyFiltering( rootQuerySpec, rootTableGroup, attributeMapping, sqlAstCreationState );
applyOrdering( rootTableGroup, attributeMapping ); applyOrdering( rootQuerySpec, rootTableGroup, attributeMapping, sqlAstCreationState );
// register the jdbc-parameters // register the jdbc-parameters
// todo (6.0) : analyzing the call paths, it seems like `jdbcParameterConsumer` // todo (6.0) : analyzing the call paths, it seems like `jdbcParameterConsumer`