@@ -751,61 +751,51 @@ private ImmutableFetchList visitFetches(FetchParent fetchParent, LoaderSqlAstCre
751
751
}
752
752
753
753
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 );
755
755
756
756
final FetchableContainer referencedMappingContainer = fetchParent .getReferencedMappingContainer ();
757
-
758
- final List <Fetchable > bagKeyFetchables = new ArrayList ();
759
757
if ( fetchParent .getNavigablePath ().getParent () != null ) {
760
758
final int size = referencedMappingContainer .getNumberOfKeyFetchables ();
761
759
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 );
769
761
}
770
762
}
771
763
772
764
final int size = referencedMappingContainer .getNumberOfFetchables ();
773
- final List <Fetchable > bagFetchables = new ArrayList ();
774
-
765
+ final List <Fetchable > bagFetchables = new ArrayList <>();
775
766
for ( int i = 0 ; i < size ; i ++ ) {
776
- Fetchable fetchable = referencedMappingContainer .getFetchable ( i );
767
+ final Fetchable fetchable = referencedMappingContainer .getFetchable ( i );
777
768
if ( isBag ( fetchable ) ) {
769
+ // Delay processing of bag fetchables at last since they cannot be joined and will create subsequent selects
778
770
bagFetchables .add ( fetchable );
779
771
}
780
772
else {
781
773
processor .accept ( fetchable , false , false );
782
774
}
783
775
}
784
-
785
- for ( Fetchable fetchable : bagKeyFetchables ) {
786
- processor .accept ( fetchable , true , true );
787
- }
788
776
for ( Fetchable fetchable : bagFetchables ) {
789
777
processor .accept ( fetchable , false , true );
790
778
}
791
779
return fetches .build ();
792
780
}
793
781
794
782
private boolean isBag (Fetchable fetchable ) {
795
- if ( fetchable instanceof PluralAttributeMapping ) {
796
- return ( (PluralAttributeMapping ) fetchable ).getMappedType ()
783
+ return isPluralAttributeMapping ( fetchable ) && ( (PluralAttributeMapping ) fetchable ).getMappedType ()
797
784
.getCollectionSemantics ()
798
785
.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 ();
801
791
}
802
792
803
793
@ 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 );
806
796
}
807
797
808
- private FetchableConsumer < Fetchable , Boolean , Boolean > createFetchableConsumer (
798
+ private FetchableConsumer createFetchableConsumer (
809
799
FetchParent fetchParent ,
810
800
LoaderSqlAstCreationState creationState ,
811
801
ImmutableFetchList .Builder fetches ) {
@@ -860,7 +850,7 @@ private FetchableConsumer<Fetchable, Boolean, Boolean> createFetchableConsumer(
860
850
boolean explicitFetch = false ;
861
851
EntityGraphTraversalState .TraversalResult traversalResult = null ;
862
852
863
- final boolean isFetchablePluralAttributeMapping = fetchable instanceof PluralAttributeMapping ;
853
+ final boolean isFetchablePluralAttributeMapping = isABag || isPluralAttributeMapping ( fetchable ) ;
864
854
final Integer maximumFetchDepth = creationContext .getMaximumFetchDepth ();
865
855
866
856
if ( !( fetchable instanceof CollectionPart ) ) {
@@ -911,8 +901,8 @@ else if ( loadQueryInfluencers.getEnabledCascadingFetchProfile() != null ) {
911
901
}
912
902
}
913
903
final String previousBagRole = currentBagRole ;
914
- // whenever there is a joined bag if nay another collection is joined then the bag will contain duplicate results
915
904
if ( isABag ) {
905
+ // Avoid joining bag collections to other bags or if any other collection was joined to avoid result duplication
916
906
if ( joined && ( hasCollectionJoinFetches || currentBagRole != null ) ) {
917
907
joined = false ;
918
908
}
0 commit comments