HHH-18436 Apply plural attribute ordering when creating the join
This commit is contained in:
parent
b61628326c
commit
93e74362d5
|
@ -962,19 +962,6 @@ public class LoaderSelectBuilder {
|
|||
creationState
|
||||
);
|
||||
|
||||
if ( fetch.getTiming() == FetchTiming.IMMEDIATE && joined ) {
|
||||
if ( isFetchablePluralAttributeMapping ) {
|
||||
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) fetchable;
|
||||
final QuerySpec querySpec = creationState.getInflightQueryPart().getFirstQuerySpec();
|
||||
applyOrdering(
|
||||
querySpec,
|
||||
fetchablePath,
|
||||
pluralAttributeMapping,
|
||||
creationState
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fetches.add( fetch );
|
||||
}
|
||||
finally {
|
||||
|
@ -1009,19 +996,6 @@ public class LoaderSelectBuilder {
|
|||
return true;
|
||||
}
|
||||
|
||||
private void applyOrdering(
|
||||
QuerySpec querySpec,
|
||||
NavigablePath navigablePath,
|
||||
PluralAttributeMapping pluralAttributeMapping,
|
||||
LoaderSqlAstCreationState sqlAstCreationState) {
|
||||
assert pluralAttributeMapping.getAttributeName().equals( navigablePath.getLocalName() );
|
||||
|
||||
final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().getTableGroup( navigablePath );
|
||||
assert tableGroup != null;
|
||||
|
||||
applyOrdering( querySpec, tableGroup, pluralAttributeMapping, sqlAstCreationState );
|
||||
}
|
||||
|
||||
private SelectStatement generateSelect(SubselectFetch subselect) {
|
||||
|
||||
// todo (6.0) : we could even convert this to a join by piecing together
|
||||
|
|
|
@ -24,6 +24,7 @@ import org.hibernate.graph.spi.AppliedGraph;
|
|||
import org.hibernate.metamodel.mapping.AssociationKey;
|
||||
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
|
||||
import org.hibernate.metamodel.mapping.ModelPart;
|
||||
import org.hibernate.metamodel.mapping.ordering.OrderByFragment;
|
||||
import org.hibernate.query.spi.Limit;
|
||||
import org.hibernate.query.sqm.tree.from.SqmFrom;
|
||||
import org.hibernate.spi.NavigablePath;
|
||||
|
@ -41,8 +42,10 @@ import org.hibernate.sql.ast.spi.SqlAstProcessingState;
|
|||
import org.hibernate.sql.ast.spi.SqlAstQueryPartProcessingState;
|
||||
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
|
||||
import org.hibernate.sql.ast.tree.from.FromClause;
|
||||
import org.hibernate.sql.ast.tree.from.TableGroup;
|
||||
import org.hibernate.sql.ast.tree.predicate.Predicate;
|
||||
import org.hibernate.sql.ast.tree.select.QueryPart;
|
||||
import org.hibernate.sql.ast.tree.select.QuerySpec;
|
||||
import org.hibernate.sql.results.graph.DomainResultCreationState;
|
||||
import org.hibernate.sql.results.graph.FetchParent;
|
||||
import org.hibernate.sql.results.graph.internal.ImmutableFetchList;
|
||||
|
@ -94,6 +97,13 @@ public class LoaderSqlAstCreationState
|
|||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyOrdering(TableGroup tableGroup, OrderByFragment orderByFragment) {
|
||||
final QuerySpec querySpec = getInflightQueryPart().getFirstQuerySpec();
|
||||
assert querySpec.isRoot() : "Illegal attempt to apply order-by fragment to a non-root query spec";
|
||||
orderByFragment.apply( querySpec, tableGroup, this );
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlAstCreationContext getCreationContext() {
|
||||
return sf;
|
||||
|
|
|
@ -737,6 +737,16 @@ public class PluralAttributeMappingImpl
|
|||
creationState
|
||||
);
|
||||
|
||||
if ( fetched ) {
|
||||
if ( orderByFragment != null ) {
|
||||
creationState.applyOrdering( tableGroup, orderByFragment );
|
||||
}
|
||||
|
||||
if ( manyToManyOrderByFragment != null ) {
|
||||
creationState.applyOrdering( tableGroup, manyToManyOrderByFragment );
|
||||
}
|
||||
}
|
||||
|
||||
final TableGroupJoin tableGroupJoin = new TableGroupJoin(
|
||||
navigablePath,
|
||||
determineSqlJoinType( lhs, requestedJoinType, fetched ),
|
||||
|
|
|
@ -8555,14 +8555,6 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
|
|||
|
||||
@Override
|
||||
public ImmutableFetchList visitFetches(FetchParent fetchParent) {
|
||||
if ( fetchParent instanceof EagerCollectionFetch && currentQuerySpec().isRoot() ) {
|
||||
final EagerCollectionFetch collectionFetch = (EagerCollectionFetch) fetchParent;
|
||||
final PluralAttributeMapping pluralAttributeMapping = collectionFetch.getFetchedMapping();
|
||||
final NavigablePath fetchablePath = collectionFetch.getNavigablePath();
|
||||
final TableGroup tableGroup = getFromClauseIndex().getTableGroup( fetchablePath );
|
||||
assert tableGroup.getModelPart() == pluralAttributeMapping;
|
||||
applyOrdering( tableGroup, pluralAttributeMapping );
|
||||
}
|
||||
final FetchableContainer referencedMappingContainer = fetchParent.getReferencedMappingContainer();
|
||||
final int keySize = referencedMappingContainer.getNumberOfKeyFetchables();
|
||||
final int size = referencedMappingContainer.getNumberOfFetchables();
|
||||
|
@ -8649,12 +8641,15 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
|
|||
}
|
||||
}
|
||||
|
||||
private void applyOrdering(TableGroup tableGroup, OrderByFragment orderByFragment) {
|
||||
@Override
|
||||
public void applyOrdering(TableGroup tableGroup, OrderByFragment orderByFragment) {
|
||||
if ( currentQuerySpec().isRoot() ) {
|
||||
if ( orderByFragments == null ) {
|
||||
orderByFragments = new ArrayList<>();
|
||||
}
|
||||
orderByFragments.add( new AbstractMap.SimpleEntry<>( orderByFragment, tableGroup ) );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S, M> M resolveMetadata(S source, Function<S, M> producer ) {
|
||||
|
|
|
@ -9,6 +9,7 @@ package org.hibernate.sql.ast.spi;
|
|||
import org.hibernate.Internal;
|
||||
import org.hibernate.LockMode;
|
||||
import org.hibernate.engine.spi.LoadQueryInfluencers;
|
||||
import org.hibernate.metamodel.mapping.ordering.OrderByFragment;
|
||||
import org.hibernate.persister.entity.EntityNameUse;
|
||||
import org.hibernate.sql.ast.tree.from.TableGroup;
|
||||
|
||||
|
@ -49,4 +50,8 @@ public interface SqlAstCreationState {
|
|||
default boolean supportsEntityNameUsage() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Internal
|
||||
default void applyOrdering(TableGroup tableGroup, OrderByFragment orderByFragment) {
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue