From 9633345dbb4c6094198e042d5d5c12dfb4db3438 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Sat, 1 Feb 2020 09:10:20 +0000 Subject: [PATCH] Fix issue determining fetchablePath and fetchParent for PluralAttributes in LoaderSelectBuilder#createFetchableConsumer() method --- .../ast/internal/LoaderSelectBuilder.java | 115 +++++++----------- 1 file changed, 47 insertions(+), 68 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSelectBuilder.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSelectBuilder.java index 7aee43dea9..0d43d70c08 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSelectBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderSelectBuilder.java @@ -397,86 +397,65 @@ public class LoaderSelectBuilder { QuerySpec querySpec, LoaderSqlAstCreationState creationState, List fetches) { return fetchable -> { - final NavigablePath fetchablePath; - final Fetchable fetchedFetchable; - if ( fetchable instanceof PluralAttributeMapping ) { - fetchablePath = fetchParent.getNavigablePath() - .append( fetchable.getFetchableName() ) - .append( CollectionPart.Nature.ELEMENT.getName() ); - fetchedFetchable = ( (PluralAttributeMapping) fetchable ).getElementDescriptor(); - } - else { - fetchablePath = fetchParent.getNavigablePath().append( fetchable.getFetchableName() ); - fetchedFetchable = fetchable; - } - final Fetch biDirectionalFetch = fetchedFetchable.resolveCircularFetch( - fetchablePath, - fetchParent, - creationState - ); + final NavigablePath fetchablePath = fetchParent.getNavigablePath().append( fetchable.getFetchableName() ); - if ( biDirectionalFetch != null ) { - fetches.add( biDirectionalFetch ); + final Fetch biDirectionalFetch = fetchable.resolveCircularFetch( + fetchablePath, + fetchParent, + creationState + ); + + if ( biDirectionalFetch != null ) { + fetches.add( biDirectionalFetch ); + return; + } + + LockMode lockMode = LockMode.READ; + FetchTiming fetchTiming = fetchable.getMappedFetchStrategy().getTiming(); + boolean joined = fetchable.getMappedFetchStrategy().getStyle() == FetchStyle.JOIN; + + final Integer maximumFetchDepth = creationContext.getMaximumFetchDepth(); + + if ( maximumFetchDepth != null ) { + if ( fetchDepth == maximumFetchDepth ) { + joined = false; + } + else if ( fetchDepth > maximumFetchDepth ) { return; } + } - LockMode lockMode = LockMode.READ; - FetchTiming fetchTiming = fetchable.getMappedFetchStrategy().getTiming(); - boolean joined = fetchable.getMappedFetchStrategy().getStyle() == FetchStyle.JOIN; - - // if ( loadable instanceof PluralValuedNavigable ) { - // // processing a collection-loader - // - // // if the `fetchable` is the "collection owner" and the collection owner is available in Session - don't join - // final String collectionMappedByProperty = ( (PluralValuedNavigable) rootContainer ).getCollectionDescriptor() - // .getMappedByProperty(); - // if ( collectionMappedByProperty != null && collectionMappedByProperty.equals( fetchable.getNavigableName() ) ) { - // joined = false; - // } - // } - - final Integer maximumFetchDepth = creationContext.getMaximumFetchDepth(); - - if ( maximumFetchDepth != null ) { - if ( fetchDepth == maximumFetchDepth ) { - joined = false; - } - else if ( fetchDepth > maximumFetchDepth ) { - return; - } + try { + if ( !( fetchable instanceof BasicValuedModelPart ) ) { + fetchDepth++; } + Fetch fetch = fetchable.generateFetch( + fetchParent, + fetchablePath, + fetchTiming, + joined, + lockMode, + null, + creationState + ); + fetches.add( fetch ); - try { - if ( ! (fetchable instanceof BasicValuedModelPart ) ) { - fetchDepth++; - } - Fetch fetch = fetchable.generateFetch( - fetchParent, + if ( fetchable instanceof PluralAttributeMapping && fetchTiming == FetchTiming.IMMEDIATE ) { + applyOrdering( + querySpec, fetchablePath, - fetchTiming, - joined, - lockMode, - null, + ( (PluralAttributeMapping) fetchable ), creationState ); - fetches.add( fetch ); - - if ( fetchable instanceof PluralAttributeMapping && fetchTiming == FetchTiming.IMMEDIATE ) { - applyOrdering( - querySpec, - fetchablePath, - ( (PluralAttributeMapping) fetchable ), - creationState - ); - } } - finally { - if ( ! (fetchable instanceof BasicValuedModelPart) ) { - fetchDepth--; - } + } + finally { + if ( !( fetchable instanceof BasicValuedModelPart ) ) { + fetchDepth--; } - }; + } + }; } private void applyOrdering(