Skip to content

Commit be77318

Browse files
mbelladebeikov
authored andcommitted
HHH-16453 Small changes for bag fetchables in LoaderSelectBuilder
1 parent ab3986a commit be77318

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSelectBuilder.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -751,61 +751,51 @@ private ImmutableFetchList visitFetches(FetchParent fetchParent, LoaderSqlAstCre
751751
}
752752

753753
final ImmutableFetchList.Builder fetches = new ImmutableFetchList.Builder( fetchParent.getReferencedMappingContainer() );
754-
final FetchableConsumer<Fetchable, Boolean, Boolean> processor = createFetchableConsumer( fetchParent, creationState, fetches );
754+
final FetchableConsumer processor = createFetchableConsumer( fetchParent, creationState, fetches );
755755

756756
final FetchableContainer referencedMappingContainer = fetchParent.getReferencedMappingContainer();
757-
758-
final List<Fetchable> bagKeyFetchables = new ArrayList();
759757
if ( fetchParent.getNavigablePath().getParent() != null ) {
760758
final int size = referencedMappingContainer.getNumberOfKeyFetchables();
761759
for ( int i = 0; i < size; i++ ) {
762-
Fetchable keyFetchable = referencedMappingContainer.getKeyFetchable( i );
763-
if ( isBag( keyFetchable ) ) {
764-
bagKeyFetchables.add( keyFetchable );
765-
}
766-
else {
767-
processor.accept( keyFetchable, true, false );
768-
}
760+
processor.accept( referencedMappingContainer.getKeyFetchable( i ), true, false );
769761
}
770762
}
771763

772764
final int size = referencedMappingContainer.getNumberOfFetchables();
773-
final List<Fetchable> bagFetchables = new ArrayList();
774-
765+
final List<Fetchable> bagFetchables = new ArrayList<>();
775766
for ( int i = 0; i < size; i++ ) {
776-
Fetchable fetchable = referencedMappingContainer.getFetchable( i );
767+
final Fetchable fetchable = referencedMappingContainer.getFetchable( i );
777768
if ( isBag( fetchable ) ) {
769+
// Delay processing of bag fetchables at last since they cannot be joined and will create subsequent selects
778770
bagFetchables.add( fetchable );
779771
}
780772
else {
781773
processor.accept( fetchable, false, false );
782774
}
783775
}
784-
785-
for ( Fetchable fetchable : bagKeyFetchables ) {
786-
processor.accept( fetchable, true, true );
787-
}
788776
for ( Fetchable fetchable : bagFetchables ) {
789777
processor.accept( fetchable, false, true );
790778
}
791779
return fetches.build();
792780
}
793781

794782
private boolean isBag(Fetchable fetchable) {
795-
if ( fetchable instanceof PluralAttributeMapping ) {
796-
return ( (PluralAttributeMapping) fetchable ).getMappedType()
783+
return isPluralAttributeMapping( fetchable ) && ( (PluralAttributeMapping) fetchable ).getMappedType()
797784
.getCollectionSemantics()
798785
.getCollectionClassification() == CollectionClassification.BAG;
799-
}
800-
return false;
786+
}
787+
788+
private boolean isPluralAttributeMapping(Fetchable fetchable) {
789+
final AttributeMapping attributeMapping = fetchable.asAttributeMapping();
790+
return attributeMapping != null && attributeMapping.isPluralAttributeMapping();
801791
}
802792

803793
@FunctionalInterface
804-
private interface FetchableConsumer<F,T,V>{
805-
void accept(F fetchable, T isKeyFetchable, V isABag);
794+
private interface FetchableConsumer {
795+
void accept(Fetchable fetchable, boolean isKeyFetchable, boolean isABag);
806796
}
807797

808-
private FetchableConsumer<Fetchable, Boolean, Boolean> createFetchableConsumer(
798+
private FetchableConsumer createFetchableConsumer(
809799
FetchParent fetchParent,
810800
LoaderSqlAstCreationState creationState,
811801
ImmutableFetchList.Builder fetches) {
@@ -860,7 +850,7 @@ private FetchableConsumer<Fetchable, Boolean, Boolean> createFetchableConsumer(
860850
boolean explicitFetch = false;
861851
EntityGraphTraversalState.TraversalResult traversalResult = null;
862852

863-
final boolean isFetchablePluralAttributeMapping = fetchable instanceof PluralAttributeMapping;
853+
final boolean isFetchablePluralAttributeMapping = isABag || isPluralAttributeMapping( fetchable );
864854
final Integer maximumFetchDepth = creationContext.getMaximumFetchDepth();
865855

866856
if ( !( fetchable instanceof CollectionPart ) ) {
@@ -911,8 +901,8 @@ else if ( loadQueryInfluencers.getEnabledCascadingFetchProfile() != null ) {
911901
}
912902
}
913903
final String previousBagRole = currentBagRole;
914-
// whenever there is a joined bag if nay another collection is joined then the bag will contain duplicate results
915904
if ( isABag ) {
905+
// Avoid joining bag collections to other bags or if any other collection was joined to avoid result duplication
916906
if ( joined && ( hasCollectionJoinFetches || currentBagRole != null ) ) {
917907
joined = false;
918908
}

0 commit comments

Comments
 (0)