HHH-16210 Avoid join fetching multiple bag-type collections

This commit is contained in:
Marco Belladelli 2023-03-13 17:08:02 +01:00 committed by Christian Beikov
parent 8700f6f93b
commit 615267f61d
2 changed files with 12 additions and 15 deletions

View File

@ -29,6 +29,7 @@ import org.hibernate.graph.GraphSemantic;
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.loader.ast.spi.Loadable;
import org.hibernate.loader.ast.spi.Loader;
import org.hibernate.metamodel.CollectionClassification;
import org.hibernate.metamodel.mapping.BasicValuedModelPart;
import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
@ -793,7 +794,8 @@ public class LoaderSelectBuilder {
final String bagRole;
if ( isFetchablePluralAttributeMapping
&& ( (PluralAttributeMapping) fetchable ).getMappedType()
.getCollectionSemantics() instanceof BagSemantics ) {
.getCollectionSemantics()
.getCollectionClassification() == CollectionClassification.BAG ) {
bagRole = fetchable.getNavigableRole().getNavigableName();
}
else {
@ -801,7 +803,7 @@ public class LoaderSelectBuilder {
}
if ( joined && previousBagRole != null && bagRole != null ) {
// Avoid join fetching multiple bags
// Avoid join fetching multiple bags to prevent result multiplication
joined = false;
}

View File

@ -467,26 +467,21 @@ public class PluralAttributeMappingImpl
private Fetch createSelectEagerCollectionFetch(
FetchParent fetchParent,
NavigablePath fetchablePath,
DomainResultCreationState creationState, SqlAstCreationState sqlAstCreationState) {
DomainResultCreationState creationState,
SqlAstCreationState sqlAstCreationState) {
final DomainResult<?> collectionKeyDomainResult;
if ( referencedPropertyName != null ) {
resolveCollectionTableGroup(
fetchParent,
fetchablePath,
creationState,
sqlAstCreationState
);
final DomainResult<?> collectionKeyDomainResult = getKeyDescriptor().createTargetDomainResult(
collectionKeyDomainResult = getKeyDescriptor().createTargetDomainResult(
fetchablePath,
sqlAstCreationState.getFromClauseAccess().getTableGroup( fetchParent.getNavigablePath() ),
fetchParent,
creationState
);
return new SelectEagerCollectionFetch( fetchablePath, this, collectionKeyDomainResult, fetchParent );
}
return new SelectEagerCollectionFetch( fetchablePath, this, null, fetchParent );
else {
collectionKeyDomainResult = null;
}
return new SelectEagerCollectionFetch( fetchablePath, this, collectionKeyDomainResult, fetchParent );
}
private TableGroup resolveCollectionTableGroup(