Search in sources :

Example 1 with EntityIdentifierNavigablePath

use of org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath in project hibernate-orm by hibernate.

the class ToOneAttributeMapping method resolveCircularFetch.

@Override
public Fetch resolveCircularFetch(NavigablePath fetchablePath, FetchParent fetchParent, FetchTiming fetchTiming, DomainResultCreationState creationState) {
    final AssociationKey associationKey = foreignKeyDescriptor.getAssociationKey();
    if (creationState.isAssociationKeyVisited(associationKey) || bidirectionalAttributeName != null && !creationState.isRegisteringVisitedAssociationKeys()) {
        NavigablePath parentNavigablePath = fetchablePath.getParent();
        assert parentNavigablePath.equals(fetchParent.getNavigablePath());
        /*
				@Entity
				public class Card {
					@Id
					private String id;
					@ManyToOne
					private CardField field;
				}
				@Entity
				public class CardField {
					@EmbeddedId
					private PrimaryKey primaryKey;
				}
				@Embeddable
				public class PrimaryKey {
					@ManyToOne(optional = false)
					private Card card;
					@ManyToOne(optional = false)
					private Key key;
				}
			 */
        if (parentNavigablePath.getLocalName().equals(ForeignKeyDescriptor.PART_NAME)) {
            // todo (6.0): maybe it's better to have a flag in creation state that marks if we are building a circular fetch domain result already to skip this?
            return null;
        }
        ModelPart parentModelPart = creationState.resolveModelPart(parentNavigablePath);
        if (parentModelPart instanceof EmbeddedIdentifierMappingImpl) {
            while (parentNavigablePath instanceof EntityIdentifierNavigablePath) {
                parentNavigablePath = parentNavigablePath.getParent();
                assert parentNavigablePath != null;
                parentModelPart = creationState.resolveModelPart(parentNavigablePath);
            }
        }
        while (parentModelPart instanceof EmbeddableValuedFetchable) {
            parentNavigablePath = parentNavigablePath.getParent();
            assert parentNavigablePath != null;
            parentModelPart = creationState.resolveModelPart(parentNavigablePath);
        }
        if (isBidirectionalAttributeName(parentNavigablePath, parentModelPart, fetchablePath, creationState)) {
            return createCircularBiDirectionalFetch(fetchablePath, fetchParent, parentNavigablePath, creationState);
        }
        /*
						class Child {
							@OneToOne
							private Mother mother;
						}

						class Mother {
							@OneToOne
							private Child stepMother;
						}

				We have a circularity but it is not bidirectional
			 */
        final TableGroup parentTableGroup = creationState.getSqlAstCreationState().getFromClauseAccess().getTableGroup(fetchParent.getNavigablePath());
        final DomainResult<?> foreignKeyDomainResult;
        assert !creationState.isResolvingCircularFetch();
        try {
            creationState.setResolvingCircularFetch(true);
            foreignKeyDomainResult = foreignKeyDescriptor.createDomainResult(fetchablePath, parentTableGroup, sideNature, creationState);
        } finally {
            creationState.setResolvingCircularFetch(false);
        }
        return new CircularFetchImpl(this, getEntityMappingType(), fetchTiming, fetchablePath, fetchParent, this, isSelectByUniqueKey(sideNature), fetchablePath, foreignKeyDomainResult);
    }
    return null;
}
Also used : AssociationKey(org.hibernate.metamodel.mapping.AssociationKey) TreatedNavigablePath(org.hibernate.query.spi.TreatedNavigablePath) NavigablePath(org.hibernate.query.spi.NavigablePath) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup) MappedByTableGroup(org.hibernate.sql.ast.tree.from.MappedByTableGroup) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) StandardTableGroup(org.hibernate.sql.ast.tree.from.StandardTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) CircularFetchImpl(org.hibernate.sql.results.internal.domain.CircularFetchImpl) EmbeddableValuedFetchable(org.hibernate.sql.results.graph.embeddable.EmbeddableValuedFetchable)

Example 2 with EntityIdentifierNavigablePath

use of org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath in project hibernate-orm by hibernate.

the class FetchParent method resolveNavigablePath.

default NavigablePath resolveNavigablePath(Fetchable fetchable) {
    final String fetchableName = fetchable.getFetchableName();
    if (NavigablePath.IDENTIFIER_MAPPER_PROPERTY.equals(fetchableName)) {
        return new EntityIdentifierNavigablePath(getNavigablePath(), fetchableName);
    } else {
        final FetchableContainer referencedMappingContainer = getReferencedMappingContainer();
        final EntityMappingType fetchableEntityType = fetchable.findContainingEntityMapping();
        final EntityMappingType fetchParentType;
        if (referencedMappingContainer instanceof EmbeddableMappingType || referencedMappingContainer instanceof EmbeddableValuedModelPart) {
            fetchParentType = referencedMappingContainer.findContainingEntityMapping();
        } else if (referencedMappingContainer instanceof EntityMappingType) {
            fetchParentType = (EntityMappingType) referencedMappingContainer;
        } else {
            fetchParentType = fetchableEntityType;
        }
        if (fetchParentType != fetchableEntityType) {
            // todo (6.0): if the fetchParentType is a subtype of fetchableEntityType this shouldn't be necessary
            return getNavigablePath().treatAs(fetchableEntityType.getEntityName()).append(fetchableName);
        }
        return getNavigablePath().append(fetchableName);
    }
}
Also used : EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Example 3 with EntityIdentifierNavigablePath

use of org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath 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);
            }
        }
    };
}
Also used : Arrays(java.util.Arrays) CollectionFetch(org.hibernate.sql.results.graph.collection.internal.CollectionFetch) CollectionDomainResult(org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult) GraphSemantic(org.hibernate.graph.GraphSemantic) ResultsHelper.attributeName(org.hibernate.query.results.ResultsHelper.attributeName) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) CascadingAction(org.hibernate.engine.spi.CascadingAction) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) EntityResultImpl(org.hibernate.sql.results.graph.entity.internal.EntityResultImpl) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) BagSemantics(org.hibernate.collection.spi.BagSemantics) FetchableContainer(org.hibernate.sql.results.graph.FetchableContainer) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) BiDirectionalFetch(org.hibernate.sql.results.graph.BiDirectionalFetch) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) Map(java.util.Map) FetchProfile(org.hibernate.engine.profile.FetchProfile) JdbcParameterImpl(org.hibernate.sql.exec.internal.JdbcParameterImpl) ComparisonOperator(org.hibernate.query.sqm.ComparisonOperator) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) Fetchable(org.hibernate.sql.results.graph.Fetchable) TableGroupJoinProducer(org.hibernate.sql.ast.tree.from.TableGroupJoinProducer) LockOptions(org.hibernate.LockOptions) NavigablePath(org.hibernate.query.spi.NavigablePath) AliasCollector(org.hibernate.sql.ast.spi.AliasCollector) DomainResult(org.hibernate.sql.results.graph.DomainResult) Expression(org.hibernate.sql.ast.tree.expression.Expression) OrderByFragment(org.hibernate.metamodel.mapping.ordering.OrderByFragment) SimpleFromClauseAccessImpl(org.hibernate.sql.ast.spi.SimpleFromClauseAccessImpl) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Objects(java.util.Objects) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) List(java.util.List) CascadeStyle(org.hibernate.engine.spi.CascadeStyle) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) SimpleForeignKeyDescriptor(org.hibernate.metamodel.mapping.internal.SimpleForeignKeyDescriptor) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) SqlExpressionResolver.createColumnReferenceKey(org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey) FetchParent(org.hibernate.sql.results.graph.FetchParent) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) Logger(org.jboss.logging.Logger) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) ArrayList(java.util.ArrayList) RootGraphImplementor(org.hibernate.graph.spi.RootGraphImplementor) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ModelPart(org.hibernate.metamodel.mapping.ModelPart) Loader(org.hibernate.loader.ast.spi.Loader) BiConsumer(java.util.function.BiConsumer) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) SqlAliasBaseManager(org.hibernate.sql.ast.spi.SqlAliasBaseManager) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FetchTiming(org.hibernate.engine.FetchTiming) Loadable(org.hibernate.loader.ast.spi.Loadable) Fetch(org.hibernate.sql.results.graph.Fetch) Consumer(java.util.function.Consumer) Restrictable(org.hibernate.metamodel.mapping.Restrictable) EntityGraphTraversalState(org.hibernate.sql.results.graph.EntityGraphTraversalState) FetchStyle(org.hibernate.engine.FetchStyle) StandardEntityGraphTraversalStateImpl(org.hibernate.sql.results.internal.StandardEntityGraphTraversalStateImpl) AbstractMap(java.util.AbstractMap) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) QueryPart(org.hibernate.sql.ast.tree.select.QueryPart) SqlAstJoinType(org.hibernate.sql.ast.SqlAstJoinType) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) Collections(java.util.Collections) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) EffectiveEntityGraph(org.hibernate.engine.spi.EffectiveEntityGraph) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) CascadeStyle(org.hibernate.engine.spi.CascadeStyle) CascadingAction(org.hibernate.engine.spi.CascadingAction) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) Fetchable(org.hibernate.sql.results.graph.Fetchable) NavigablePath(org.hibernate.query.spi.NavigablePath) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) CollectionFetch(org.hibernate.sql.results.graph.collection.internal.CollectionFetch) BiDirectionalFetch(org.hibernate.sql.results.graph.BiDirectionalFetch) SubselectFetch(org.hibernate.engine.spi.SubselectFetch) Fetch(org.hibernate.sql.results.graph.Fetch) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) BiDirectionalFetch(org.hibernate.sql.results.graph.BiDirectionalFetch) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) FetchProfile(org.hibernate.engine.profile.FetchProfile) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) FetchableContainer(org.hibernate.sql.results.graph.FetchableContainer) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) FetchTiming(org.hibernate.engine.FetchTiming) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) BagSemantics(org.hibernate.collection.spi.BagSemantics)

Example 4 with EntityIdentifierNavigablePath

use of org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath in project hibernate-orm by hibernate.

the class DomainResultCreationStateImpl method visitFetches.

@Override
public List<Fetch> visitFetches(FetchParent fetchParent) {
    final FetchableContainer fetchableContainer = fetchParent.getReferencedMappingContainer();
    final List<Fetch> fetches = CollectionHelper.arrayList(fetchableContainer.getNumberOfFetchables());
    final Consumer<Fetchable> fetchableConsumer = fetchable -> {
        final String fetchableName = fetchable.getFetchableName();
        Map.Entry<String, NavigablePath> currentEntry;
        if (relativePathStack.isEmpty()) {
            currentEntry = new AbstractMap.SimpleEntry<>(getRelativePath("", fetchable, fetchableContainer), new NavigablePath(fetchableName));
        } else {
            final Map.Entry<String, NavigablePath> oldEntry = relativePathStack.getCurrent();
            final String key = oldEntry.getKey();
            currentEntry = new AbstractMap.SimpleEntry<>(getRelativePath(key, fetchable, fetchableContainer), oldEntry.getValue().append(fetchableName));
        }
        // todo (6.0): figure out if we can somehow create the navigable paths in a better way
        final String fullPath = currentEntry.getKey();
        FetchBuilder explicitFetchBuilder = fetchBuilderResolverStack.getCurrent().apply(fullPath);
        DynamicFetchBuilderLegacy fetchBuilderLegacy;
        if (explicitFetchBuilder == null) {
            fetchBuilderLegacy = legacyFetchResolver.resolve(fromClauseAccess.findTableGroup(fetchParent.getNavigablePath()).getPrimaryTableReference().getIdentificationVariable(), fetchableName);
        } else {
            fetchBuilderLegacy = null;
        }
        if (fetchable instanceof Association && fetchable.getMappedFetchOptions().getTiming() == FetchTiming.DELAYED) {
            final Association association = (Association) fetchable;
            final ForeignKeyDescriptor foreignKeyDescriptor = association.getForeignKeyDescriptor();
            final String partName = attributeName(foreignKeyDescriptor.getSide(association.getSideNature().inverse()).getModelPart());
            // If there are no fetch builders for this association, we only want to fetch the FK
            if (explicitFetchBuilder == null && fetchBuilderLegacy == null && partName != null) {
                currentEntry = new AbstractMap.SimpleEntry<>(currentEntry.getKey() + "." + partName, currentEntry.getValue().append(partName));
                explicitFetchBuilder = fetchBuilderResolverStack.getCurrent().apply(currentEntry.getKey());
                if (explicitFetchBuilder == null) {
                    fetchBuilderLegacy = legacyFetchResolver.resolve(fromClauseAccess.findTableGroup(fetchParent.getNavigablePath()).getPrimaryTableReference().getIdentificationVariable(), fetchableName);
                }
            }
        }
        relativePathStack.push(currentEntry);
        try {
            final NavigablePath fetchPath = fetchParent.resolveNavigablePath(fetchable);
            final FetchBuilder fetchBuilder;
            if (explicitFetchBuilder != null) {
                fetchBuilder = explicitFetchBuilder;
            } else {
                if (fetchBuilderLegacy == null) {
                    fetchBuilder = Builders.implicitFetchBuilder(fetchPath, fetchable, this);
                } else {
                    fetchBuilder = fetchBuilderLegacy;
                }
            }
            final Fetch fetch = fetchBuilder.buildFetch(fetchParent, fetchPath, jdbcResultsMetadata, (s, s2) -> {
                throw new UnsupportedOperationException();
            }, this);
            fetches.add(fetch);
        } finally {
            relativePathStack.pop();
        }
    };
    boolean previous = this.processingKeyFetches;
    this.processingKeyFetches = true;
    if (fetchableContainer instanceof EntityValuedModelPart) {
        final EntityValuedModelPart entityValuedFetchable = (EntityValuedModelPart) fetchableContainer;
        final EntityIdentifierMapping identifierMapping = entityValuedFetchable.getEntityMappingType().getIdentifierMapping();
        final boolean idClass = identifierMapping instanceof NonAggregatedIdentifierMapping;
        final String identifierAttributeName = attributeName(identifierMapping);
        if (idClass) {
            final Map.Entry<String, NavigablePath> oldEntry = relativePathStack.getCurrent();
            relativePathStack.push(new AbstractMap.SimpleEntry<>(oldEntry == null ? "" : oldEntry.getKey(), new EntityIdentifierNavigablePath(oldEntry == null ? fetchParent.getNavigablePath() : oldEntry.getValue(), identifierAttributeName)));
        } else if (identifierMapping instanceof CompositeIdentifierMapping) {
            final Map.Entry<String, NavigablePath> oldEntry = relativePathStack.getCurrent();
            relativePathStack.push(new AbstractMap.SimpleEntry<>(oldEntry == null ? identifierAttributeName : oldEntry.getKey() + "." + identifierAttributeName, new EntityIdentifierNavigablePath(oldEntry == null ? fetchParent.getNavigablePath() : oldEntry.getValue(), identifierAttributeName)));
        }
        try {
            if (identifierMapping instanceof FetchableContainer) {
                // essentially means the entity has a composite id - ask the embeddable to visit its fetchables
                ((FetchableContainer) identifierMapping).visitFetchables(fetchableConsumer, null);
            } else {
                fetchableConsumer.accept((Fetchable) identifierMapping);
            }
        } finally {
            this.processingKeyFetches = previous;
            if (idClass) {
                this.relativePathStack.pop();
            }
        }
    }
    fetchableContainer.visitKeyFetchables(fetchableConsumer, null);
    fetchableContainer.visitFetchables(fetchableConsumer, null);
    return fetches;
}
Also used : CollectionHelper(org.hibernate.internal.util.collections.CollectionHelper) DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) SqlAliasBaseGenerator(org.hibernate.sql.ast.spi.SqlAliasBaseGenerator) ResultsLogger(org.hibernate.sql.results.ResultsLogger) SingleAttributeIdentifierMapping(org.hibernate.metamodel.mapping.internal.SingleAttributeIdentifierMapping) CompositeIdentifierMapping(org.hibernate.metamodel.mapping.CompositeIdentifierMapping) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) JavaType(org.hibernate.type.descriptor.java.JavaType) ResultsHelper.attributeName(org.hibernate.query.results.ResultsHelper.attributeName) HashMap(java.util.HashMap) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) Function(java.util.function.Function) BasicValuedCollectionPart(org.hibernate.metamodel.mapping.internal.BasicValuedCollectionPart) Internal(org.hibernate.Internal) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) ModelPart(org.hibernate.metamodel.mapping.ModelPart) FetchableContainer(org.hibernate.sql.results.graph.FetchableContainer) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) Map(java.util.Map) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) SqlAliasBaseManager(org.hibernate.sql.ast.spi.SqlAliasBaseManager) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) Fetchable(org.hibernate.sql.results.graph.Fetchable) FetchTiming(org.hibernate.engine.FetchTiming) LockMode(org.hibernate.LockMode) AssociationKey(org.hibernate.metamodel.mapping.AssociationKey) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) LegacyFetchResolver(org.hibernate.query.results.dynamic.LegacyFetchResolver) NavigablePath(org.hibernate.query.spi.NavigablePath) Expression(org.hibernate.sql.ast.tree.expression.Expression) Fetch(org.hibernate.sql.results.graph.Fetch) SqlAstProcessingState(org.hibernate.sql.ast.spi.SqlAstProcessingState) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Consumer(java.util.function.Consumer) StandardStack(org.hibernate.internal.util.collections.StandardStack) Association(org.hibernate.metamodel.mapping.Association) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) JdbcValuesMetadata(org.hibernate.sql.results.jdbc.spi.JdbcValuesMetadata) AbstractMap(java.util.AbstractMap) List(java.util.List) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) Stack(org.hibernate.internal.util.collections.Stack) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FetchParent(org.hibernate.sql.results.graph.FetchParent) Fetchable(org.hibernate.sql.results.graph.Fetchable) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) NavigablePath(org.hibernate.query.spi.NavigablePath) CompositeIdentifierMapping(org.hibernate.metamodel.mapping.CompositeIdentifierMapping) Fetch(org.hibernate.sql.results.graph.Fetch) AbstractMap(java.util.AbstractMap) Association(org.hibernate.metamodel.mapping.Association) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) DynamicFetchBuilderLegacy(org.hibernate.query.results.dynamic.DynamicFetchBuilderLegacy) FetchableContainer(org.hibernate.sql.results.graph.FetchableContainer) NonAggregatedIdentifierMapping(org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap)

Example 5 with EntityIdentifierNavigablePath

use of org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath in project hibernate-orm by hibernate.

the class BasicValuedCollectionPart method generateFetch.

@Override
public Fetch generateFetch(FetchParent fetchParent, NavigablePath fetchablePath, FetchTiming fetchTiming, boolean selected, String resultVariable, DomainResultCreationState creationState) {
    ResultsLogger.LOGGER.debugf("Generating Fetch for collection-part : `%s` -> `%s`", collectionDescriptor.getRole(), nature.getName());
    NavigablePath parentNavigablePath = fetchablePath.getParent();
    if (parentNavigablePath instanceof EntityIdentifierNavigablePath) {
        parentNavigablePath = parentNavigablePath.getParent();
    }
    final TableGroup tableGroup = creationState.getSqlAstCreationState().getFromClauseAccess().findTableGroup(parentNavigablePath);
    final SqlSelection sqlSelection = resolveSqlSelection(fetchablePath, tableGroup, true, creationState);
    return new BasicFetch<>(sqlSelection.getValuesArrayPosition(), fetchParent, fetchablePath, this, valueConverter, FetchTiming.IMMEDIATE, creationState);
}
Also used : BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) NavigablePath(org.hibernate.query.spi.NavigablePath) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Aggregations

EntityIdentifierNavigablePath (org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath)6 NavigablePath (org.hibernate.query.spi.NavigablePath)5 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)5 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)3 ModelPart (org.hibernate.metamodel.mapping.ModelPart)3 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)3 AbstractMap (java.util.AbstractMap)2 List (java.util.List)2 Map (java.util.Map)2 Consumer (java.util.function.Consumer)2 FetchTiming (org.hibernate.engine.FetchTiming)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)2 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)2 EntityValuedModelPart (org.hibernate.metamodel.mapping.EntityValuedModelPart)2 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)2 NonAggregatedIdentifierMapping (org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping)2 ResultsHelper.attributeName (org.hibernate.query.results.ResultsHelper.attributeName)2 SqlAliasBaseManager (org.hibernate.sql.ast.spi.SqlAliasBaseManager)2 SqlAstCreationContext (org.hibernate.sql.ast.spi.SqlAstCreationContext)2