Skip to content

Commit 51b6833

Browse files
committed
HHH-15885 Fix wrong collection fetch element ordering
1 parent 44ad64b commit 51b6833

File tree

1 file changed

+53
-56
lines changed

1 file changed

+53
-56
lines changed

hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@
8989
import org.hibernate.metamodel.mapping.internal.SqlTypedMappingImpl;
9090
import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping;
9191
import org.hibernate.metamodel.mapping.ordering.OrderByFragment;
92+
import org.hibernate.sql.results.graph.collection.internal.EagerCollectionFetch;
93+
import org.hibernate.type.descriptor.converter.internal.OrdinalEnumValueConverter;
94+
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
9295
import org.hibernate.metamodel.model.domain.BasicDomainType;
9396
import org.hibernate.metamodel.model.domain.EmbeddableDomainType;
9497
import org.hibernate.metamodel.model.domain.EntityDomainType;
@@ -389,8 +392,6 @@
389392
import org.hibernate.type.EnumType;
390393
import org.hibernate.type.JavaObjectType;
391394
import org.hibernate.type.SqlTypes;
392-
import org.hibernate.type.descriptor.converter.internal.OrdinalEnumValueConverter;
393-
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
394395
import org.hibernate.type.descriptor.java.EnumJavaType;
395396
import org.hibernate.type.descriptor.java.JavaType;
396397
import org.hibernate.type.descriptor.java.JavaTypeHelper;
@@ -7283,7 +7284,6 @@ else if ( getLoadQueryInfluencers().hasEnabledFetchProfiles() ) {
72837284
fetchable,
72847285
fetchTiming,
72857286
joined,
7286-
explicitFetch,
72877287
alias
72887288
);
72897289

@@ -7335,6 +7335,55 @@ public ImmutableFetchList visitNestedFetches(FetchParent fetchParent) {
73357335

73367336
@Override
73377337
public ImmutableFetchList visitFetches(FetchParent fetchParent) {
7338+
if ( fetchParent instanceof EagerCollectionFetch ) {
7339+
final EagerCollectionFetch collectionFetch = (EagerCollectionFetch) fetchParent;
7340+
final PluralAttributeMapping pluralAttributeMapping = collectionFetch.getFetchedMapping();
7341+
final NavigablePath fetchablePath = collectionFetch.getNavigablePath();
7342+
7343+
final TableGroup tableGroup = getFromClauseIndex().getTableGroup( fetchablePath );
7344+
7345+
// Base restrictions have already been applied if this is an explicit fetch
7346+
if ( getFromClauseIndex().findFetchedJoinByPath( fetchablePath ) == null ) {
7347+
final Restrictable restrictable = pluralAttributeMapping
7348+
.getCollectionDescriptor()
7349+
.getCollectionType()
7350+
.getAssociatedJoinable( getCreationContext().getSessionFactory() );
7351+
restrictable.applyBaseRestrictions(
7352+
(predicate) -> addCollectionFilterPredicate( tableGroup.getNavigablePath(), predicate ),
7353+
tableGroup,
7354+
true,
7355+
getLoadQueryInfluencers().getEnabledFilters(),
7356+
null,
7357+
this
7358+
);
7359+
}
7360+
7361+
pluralAttributeMapping.applyBaseManyToManyRestrictions(
7362+
(predicate) -> {
7363+
final TableGroup parentTableGroup = getFromClauseIndex().getTableGroup( collectionFetch.getFetchParent().getNavigablePath() );
7364+
TableGroupJoin pluralTableGroupJoin = null;
7365+
for ( TableGroupJoin nestedTableGroupJoin : parentTableGroup.getTableGroupJoins() ) {
7366+
if ( nestedTableGroupJoin.getNavigablePath() == fetchablePath ) {
7367+
pluralTableGroupJoin = nestedTableGroupJoin;
7368+
break;
7369+
}
7370+
}
7371+
7372+
assert pluralTableGroupJoin != null;
7373+
pluralTableGroupJoin.applyPredicate( predicate );
7374+
},
7375+
tableGroup,
7376+
true,
7377+
getLoadQueryInfluencers().getEnabledFilters(),
7378+
null,
7379+
this
7380+
);
7381+
7382+
if ( currentQuerySpec().isRoot() ) {
7383+
assert tableGroup.getModelPart() == pluralAttributeMapping;
7384+
applyOrdering( tableGroup, pluralAttributeMapping );
7385+
}
7386+
}
73387387
final FetchableContainer referencedMappingContainer = fetchParent.getReferencedMappingContainer();
73397388
final int keySize = referencedMappingContainer.getNumberOfKeyFetchables();
73407389
final int size = referencedMappingContainer.getNumberOfFetchables();
@@ -7381,7 +7430,6 @@ private Fetch buildFetch(
73817430
Fetchable fetchable,
73827431
FetchTiming fetchTiming,
73837432
boolean joined,
7384-
boolean explicitFetch,
73857433
String alias) {
73867434
// fetch has access to its parent in addition to the parent having its fetches.
73877435
//
@@ -7390,65 +7438,14 @@ private Fetch buildFetch(
73907438
// "initializing" state as part of AfterLoadAction
73917439

73927440
try {
7393-
final Fetch fetch = fetchParent.generateFetchableFetch(
7441+
return fetchParent.generateFetchableFetch(
73947442
fetchable,
73957443
fetchablePath,
73967444
fetchTiming,
73977445
joined,
73987446
alias,
73997447
this
74007448
);
7401-
7402-
if ( fetchable instanceof PluralAttributeMapping
7403-
&& fetch.getTiming() == FetchTiming.IMMEDIATE
7404-
&& joined ) {
7405-
final TableGroup tableGroup = getFromClauseIndex().getTableGroup( fetchablePath );
7406-
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) fetchable;
7407-
7408-
// Base restrictions have already been applied if this is an explicit fetch
7409-
if ( !explicitFetch ) {
7410-
final Restrictable restrictable = pluralAttributeMapping
7411-
.getCollectionDescriptor()
7412-
.getCollectionType()
7413-
.getAssociatedJoinable( getCreationContext().getSessionFactory() );
7414-
restrictable.applyBaseRestrictions(
7415-
(predicate) -> addCollectionFilterPredicate( tableGroup.getNavigablePath(), predicate ),
7416-
tableGroup,
7417-
true,
7418-
getLoadQueryInfluencers().getEnabledFilters(),
7419-
null,
7420-
this
7421-
);
7422-
}
7423-
7424-
pluralAttributeMapping.applyBaseManyToManyRestrictions(
7425-
(predicate) -> {
7426-
final TableGroup parentTableGroup = getFromClauseIndex().getTableGroup( fetchParent.getNavigablePath() );
7427-
TableGroupJoin pluralTableGroupJoin = null;
7428-
for ( TableGroupJoin nestedTableGroupJoin : parentTableGroup.getTableGroupJoins() ) {
7429-
if ( nestedTableGroupJoin.getNavigablePath() == fetchablePath ) {
7430-
pluralTableGroupJoin = nestedTableGroupJoin;
7431-
break;
7432-
}
7433-
}
7434-
7435-
assert pluralTableGroupJoin != null;
7436-
pluralTableGroupJoin.applyPredicate( predicate );
7437-
},
7438-
tableGroup,
7439-
true,
7440-
getLoadQueryInfluencers().getEnabledFilters(),
7441-
null,
7442-
this
7443-
);
7444-
7445-
if ( currentQuerySpec().isRoot() ) {
7446-
assert tableGroup.getModelPart() == pluralAttributeMapping;
7447-
applyOrdering( tableGroup, pluralAttributeMapping );
7448-
}
7449-
}
7450-
7451-
return fetch;
74527449
}
74537450
catch (RuntimeException e) {
74547451
throw new HibernateException(

0 commit comments

Comments
 (0)