use of org.hibernate.sql.results.graph.EntityGraphTraversalState in project hibernate-orm by hibernate.
the class LoaderSelectBuilder method createFetchableBiConsumer.
private BiConsumer<Fetchable, Boolean> createFetchableBiConsumer(FetchParent fetchParent, QuerySpec querySpec, LoaderSqlAstCreationState creationState, List<Fetch> fetches) {
return (fetchable, isKeyFetchable) -> {
final NavigablePath fetchablePath;
if (isKeyFetchable) {
final EntityIdentifierMapping identifierMapping;
if (fetchParent instanceof BiDirectionalFetch) {
final BiDirectionalFetch parentAsBiDirectionalFetch = (BiDirectionalFetch) fetchParent;
final Fetchable biDirectionalFetchedMapping = parentAsBiDirectionalFetch.getFetchedMapping();
if (biDirectionalFetchedMapping instanceof EntityValuedFetchable) {
identifierMapping = ((EntityValuedFetchable) biDirectionalFetchedMapping).getEntityMappingType().getIdentifierMapping();
} else {
identifierMapping = null;
}
} else {
final FetchableContainer fetchableContainer = fetchParent.getReferencedMappingContainer();
if (fetchableContainer instanceof EntityValuedModelPart) {
final EntityValuedModelPart entityValuedModelPart = (EntityValuedModelPart) fetchableContainer;
identifierMapping = entityValuedModelPart.getEntityMappingType().getIdentifierMapping();
} else {
identifierMapping = null;
}
}
if (identifierMapping != null) {
fetchablePath = new EntityIdentifierNavigablePath(fetchParent.getNavigablePath(), attributeName(identifierMapping));
} else {
fetchablePath = fetchParent.resolveNavigablePath(fetchable);
}
} else {
fetchablePath = fetchParent.resolveNavigablePath(fetchable);
}
FetchTiming fetchTiming = fetchable.getMappedFetchOptions().getTiming();
boolean joined = fetchable.getMappedFetchOptions().getStyle() == FetchStyle.JOIN;
boolean explicitFetch = false;
EntityGraphTraversalState.TraversalResult traversalResult = null;
final boolean isFetchablePluralAttributeMapping = fetchable instanceof PluralAttributeMapping;
if (!(fetchable instanceof CollectionPart)) {
// 'entity graph' takes precedence over 'fetch profile'
if (entityGraphTraversalState != null) {
traversalResult = entityGraphTraversalState.traverse(fetchParent, fetchable, isKeyFetchable);
fetchTiming = traversalResult.getFetchTiming();
joined = traversalResult.isJoined();
explicitFetch = true;
} else if (loadQueryInfluencers.hasEnabledFetchProfiles()) {
// There is no point in checking the fetch profile if it can't affect this fetchable
if (fetchTiming != FetchTiming.IMMEDIATE || fetchable.incrementFetchDepth()) {
final String fetchableRole = fetchable.getNavigableRole().getFullPath();
for (String enabledFetchProfileName : loadQueryInfluencers.getEnabledFetchProfileNames()) {
final FetchProfile enabledFetchProfile = creationContext.getSessionFactory().getFetchProfile(enabledFetchProfileName);
final org.hibernate.engine.profile.Fetch profileFetch = enabledFetchProfile.getFetchByRole(fetchableRole);
if (profileFetch != null) {
fetchTiming = FetchTiming.IMMEDIATE;
joined = joined || profileFetch.getStyle() == org.hibernate.engine.profile.Fetch.Style.JOIN;
explicitFetch = true;
}
}
}
} else if (loadQueryInfluencers.getEnabledCascadingFetchProfile() != null) {
final CascadeStyle cascadeStyle = ((AttributeMapping) fetchable).getAttributeMetadataAccess().resolveAttributeMetadata(fetchable.findContainingEntityMapping()).getCascadeStyle();
final CascadingAction cascadingAction = loadQueryInfluencers.getEnabledCascadingFetchProfile().getCascadingAction();
if (cascadeStyle == null || cascadeStyle.doCascade(cascadingAction)) {
fetchTiming = FetchTiming.IMMEDIATE;
// In 5.x the CascadeEntityJoinWalker only join fetched the first collection fetch
if (isFetchablePluralAttributeMapping) {
joined = !hasCollectionJoinFetches;
} else {
joined = true;
}
}
}
}
final String previousBagRole = currentBagRole;
final String bagRole;
if (isFetchablePluralAttributeMapping && ((PluralAttributeMapping) fetchable).getMappedType().getCollectionSemantics() instanceof BagSemantics) {
bagRole = fetchable.getNavigableRole().getNavigableName();
} else {
bagRole = null;
}
if (joined && previousBagRole != null && bagRole != null) {
// Avoid join fetching multiple bags
joined = false;
}
boolean changeFetchDepth = !(fetchable instanceof BasicValuedModelPart) && !(fetchable instanceof EmbeddedAttributeMapping) && !(fetchable instanceof CollectionPart);
try {
if (changeFetchDepth) {
fetchDepth++;
}
// There is no need to check for circular fetches if this is an explicit fetch
if (!explicitFetch && !creationState.isResolvingCircularFetch()) {
final Fetch biDirectionalFetch = fetchable.resolveCircularFetch(fetchablePath, fetchParent, fetchTiming, creationState);
if (biDirectionalFetch != null) {
fetches.add(biDirectionalFetch);
return;
}
}
final Integer maximumFetchDepth = creationContext.getMaximumFetchDepth();
if (maximumFetchDepth != null) {
if (fetchDepth == maximumFetchDepth + 1) {
joined = false;
} else if (fetchDepth > maximumFetchDepth + 1) {
if (!(fetchable instanceof BasicValuedModelPart) && !(fetchable instanceof EmbeddedAttributeMapping)) {
return;
}
}
}
if (joined) {
// For join fetches we remember the currentBagRole so that we can avoid multiple bag fetches
if (bagRole != null) {
currentBagRole = bagRole;
}
} else {
// For non-join fetches, we reset the currentBagRole and set it to the previous value in the finally block
currentBagRole = null;
}
final Fetch fetch = fetchParent.generateFetchableFetch(fetchable, fetchablePath, fetchTiming, joined, null, creationState);
if (fetch.getTiming() == FetchTiming.IMMEDIATE && isFetchablePluralAttributeMapping) {
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) fetchable;
if (joined) {
hasCollectionJoinFetches = true;
final TableGroup joinTableGroup = creationState.getFromClauseAccess().getTableGroup(fetchablePath);
applyFiltering(querySpec, joinTableGroup, pluralAttributeMapping, creationState);
applyOrdering(querySpec, fetchablePath, pluralAttributeMapping, creationState);
}
}
fetches.add(fetch);
} finally {
if (changeFetchDepth) {
fetchDepth--;
}
// otherwise we could run into a multiple bag fetch situation
if (!joined) {
currentBagRole = previousBagRole;
}
if (entityGraphTraversalState != null && traversalResult != null) {
entityGraphTraversalState.backtrack(traversalResult);
}
}
};
}
use of org.hibernate.sql.results.graph.EntityGraphTraversalState in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method addFetch.
public void addFetch(List<Fetch> fetches, FetchParent fetchParent, Fetchable fetchable, Boolean isKeyFetchable) {
final NavigablePath resolvedNavigablePath = fetchParent.resolveNavigablePath(fetchable);
final String alias;
FetchTiming fetchTiming = fetchable.getMappedFetchOptions().getTiming();
boolean joined = false;
EntityGraphTraversalState.TraversalResult traversalResult = null;
final FromClauseIndex fromClauseIndex = getFromClauseIndex();
final SqmAttributeJoin<?, ?> fetchedJoin = fromClauseIndex.findFetchedJoinByPath(resolvedNavigablePath);
boolean explicitFetch = false;
final NavigablePath fetchablePath;
if (fetchedJoin != null) {
fetchablePath = fetchedJoin.getNavigablePath();
// there should be a TableGroupJoin registered for this `fetchablePath` already
assert fromClauseIndex.getTableGroup(fetchedJoin.getNavigablePath()) != null;
if (fetchedJoin.isFetched()) {
fetchTiming = FetchTiming.IMMEDIATE;
}
joined = true;
alias = fetchedJoin.getExplicitAlias();
explicitFetch = true;
} else {
fetchablePath = resolvedNavigablePath;
// there was not an explicit fetch in the SQM
alias = null;
if (!(fetchable instanceof CollectionPart)) {
if (entityGraphTraversalState != null) {
traversalResult = entityGraphTraversalState.traverse(fetchParent, fetchable, isKeyFetchable);
fetchTiming = traversalResult.getFetchTiming();
joined = traversalResult.isJoined();
explicitFetch = true;
} else if (getLoadQueryInfluencers().hasEnabledFetchProfiles()) {
// There is no point in checking the fetch profile if it can't affect this fetchable
if (fetchTiming != FetchTiming.IMMEDIATE || fetchable.incrementFetchDepth()) {
final String fetchableRole = fetchable.getNavigableRole().getFullPath();
for (String enabledFetchProfileName : getLoadQueryInfluencers().getEnabledFetchProfileNames()) {
final FetchProfile enabledFetchProfile = getCreationContext().getSessionFactory().getFetchProfile(enabledFetchProfileName);
final org.hibernate.engine.profile.Fetch profileFetch = enabledFetchProfile.getFetchByRole(fetchableRole);
if (profileFetch != null) {
fetchTiming = FetchTiming.IMMEDIATE;
joined = joined || profileFetch.getStyle() == org.hibernate.engine.profile.Fetch.Style.JOIN;
explicitFetch = true;
if (currentBagRole != null && fetchable instanceof PluralAttributeMapping) {
final CollectionClassification collectionClassification = ((PluralAttributeMapping) fetchable).getMappedType().getCollectionSemantics().getCollectionClassification();
if (collectionClassification == CollectionClassification.BAG) {
// To avoid a MultipleBagFetchException due to fetch profiles in a circular model,
// we skip join fetching in case we encounter an existing bag role
joined = false;
}
}
}
}
}
}
}
// final TableGroup existingJoinedGroup = fromClauseIndex.findTableGroup( fetchablePath );
// if ( existingJoinedGroup != null ) {
// we can use this to trigger the fetch from the joined group.
// todo (6.0) : do we want to do this though?
// On the positive side it would allow EntityGraph to use the existing TableGroup. But that ties in
// to the discussion above regarding how to handle eager and EntityGraph (JOIN versus SELECT).
// Can be problematic if the existing one is restricted
// fetchTiming = FetchTiming.IMMEDIATE;
// }
// lastly, account for any app-defined max-fetch-depth
final Integer maxDepth = getCreationContext().getMaximumFetchDepth();
if (maxDepth != null) {
if (fetchDepth >= maxDepth) {
joined = false;
}
}
if (joined && fetchable instanceof TableGroupJoinProducer) {
TableGroupJoinProducer tableGroupJoinProducer = (TableGroupJoinProducer) fetchable;
fromClauseIndex.resolveTableGroup(fetchablePath, np -> {
// generate the join
final TableGroup lhs = fromClauseIndex.getTableGroup(fetchParent.getNavigablePath());
final TableGroupJoin tableGroupJoin = ((TableGroupJoinProducer) fetchable).createTableGroupJoin(fetchablePath, lhs, alias, tableGroupJoinProducer.getDefaultSqlAstJoinType(lhs), true, false, BaseSqmToSqlAstConverter.this);
lhs.addTableGroupJoin(tableGroupJoin);
return tableGroupJoin.getJoinedGroup();
});
}
}
final boolean incrementFetchDepth = fetchable.incrementFetchDepth();
try {
if (incrementFetchDepth) {
fetchDepth++;
}
// There is no need to check for circular fetches if this is an explicit fetch
if (!explicitFetch && !isResolvingCircularFetch()) {
final Fetch biDirectionalFetch = fetchable.resolveCircularFetch(fetchablePath, fetchParent, fetchTiming, this);
if (biDirectionalFetch != null) {
fetches.add(biDirectionalFetch);
return;
}
}
final Fetch fetch = buildFetch(fetchablePath, fetchParent, fetchable, fetchTiming, joined, alias);
if (fetch != null) {
if (fetch.getTiming() == FetchTiming.IMMEDIATE && fetchable instanceof PluralAttributeMapping) {
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) fetchable;
final CollectionClassification collectionClassification = pluralAttributeMapping.getMappedType().getCollectionSemantics().getCollectionClassification();
if (collectionClassification == CollectionClassification.BAG) {
if (currentBagRole != null) {
throw new MultipleBagFetchException(Arrays.asList(currentBagRole, fetchable.getNavigableRole().getNavigableName()));
}
currentBagRole = fetchable.getNavigableRole().getNavigableName();
}
}
fetches.add(fetch);
}
} finally {
if (incrementFetchDepth) {
fetchDepth--;
}
if (entityGraphTraversalState != null && traversalResult != null) {
entityGraphTraversalState.backtrack(traversalResult);
}
}
}
Aggregations