Search in sources :

Example 11 with FromClauseAccess

use of org.hibernate.sql.ast.spi.FromClauseAccess in project hibernate-orm by hibernate.

the class DynamicResultBuilderEntityStandard method buildResultOrFetch.

private <T> T buildResultOrFetch(Function<TableGroup, T> resultOrFetchBuilder, JdbcValuesMetadata jdbcResultsMetadata, DomainResultCreationState domainResultCreationState) {
    final DomainResultCreationStateImpl creationState = impl(domainResultCreationState);
    final FromClauseAccess fromClauseAccess = domainResultCreationState.getSqlAstCreationState().getFromClauseAccess();
    final TableGroup tableGroup = fromClauseAccess.resolveTableGroup(navigablePath, np -> {
        final TableReference tableReference = entityMapping.createPrimaryTableReference(new SqlAliasBaseConstant(tableAlias), creationState.getSqlExpressionResolver(), creationState.getCreationContext());
        if (lockMode != null) {
            domainResultCreationState.getSqlAstCreationState().registerLockMode(tableAlias, lockMode);
        }
        return new TableGroupImpl(navigablePath, tableAlias, tableReference, entityMapping);
    });
    final TableReference tableReference = tableGroup.getPrimaryTableReference();
    final List<String> idColumnAliases;
    final FetchBuilder idFetchBuilder;
    if (this.idColumnNames != null) {
        idColumnAliases = this.idColumnNames;
    } else if ((idFetchBuilder = findIdFetchBuilder()) != null) {
        idColumnAliases = ((DynamicFetchBuilder) idFetchBuilder).getColumnAliases();
    } else {
        idColumnAliases = null;
    }
    if (idColumnAliases != null) {
        final EntityIdentifierMapping identifierMapping = entityMapping.getIdentifierMapping();
        identifierMapping.forEachSelectable((selectionIndex, selectableMapping) -> {
            resolveSqlSelection(idColumnAliases.get(selectionIndex), createColumnReferenceKey(tableReference, selectableMapping.getSelectionExpression()), selectableMapping.getJdbcMapping(), jdbcResultsMetadata, domainResultCreationState);
        });
    }
    if (discriminatorColumnName != null) {
        resolveSqlSelection(discriminatorColumnName, createColumnReferenceKey(tableReference, entityMapping.getDiscriminatorMapping().getSelectionExpression()), entityMapping.getDiscriminatorMapping().getJdbcMapping(), jdbcResultsMetadata, domainResultCreationState);
    }
    try {
        final NavigablePath currentRelativePath = creationState.getCurrentRelativePath();
        final String prefix;
        if (currentRelativePath == null) {
            prefix = "";
        } else {
            prefix = currentRelativePath.getFullPath().replace(ELEMENT_PREFIX, "").replace(INDEX_PREFIX, "") + ".";
        }
        creationState.pushExplicitFetchMementoResolver(relativePath -> {
            if (relativePath.startsWith(prefix)) {
                final int startIndex;
                if (relativePath.regionMatches(prefix.length(), ELEMENT_PREFIX, 0, ELEMENT_PREFIX.length())) {
                    startIndex = prefix.length() + ELEMENT_PREFIX.length();
                } else {
                    startIndex = prefix.length();
                }
                return findFetchBuilder(relativePath.substring(startIndex));
            }
            return null;
        });
        return resultOrFetchBuilder.apply(tableGroup);
    } finally {
        creationState.popExplicitFetchMementoResolver();
    }
}
Also used : DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FetchBuilder(org.hibernate.query.results.FetchBuilder) NavigablePath(org.hibernate.spi.NavigablePath) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) TableReference(org.hibernate.sql.ast.tree.from.TableReference) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) TableGroupImpl(org.hibernate.query.results.TableGroupImpl)

Example 12 with FromClauseAccess

use of org.hibernate.sql.ast.spi.FromClauseAccess in project hibernate-orm by hibernate.

the class PluralAttributeMappingImpl method resolveCollectionTableGroup.

private TableGroup resolveCollectionTableGroup(FetchParent fetchParent, NavigablePath fetchablePath, DomainResultCreationState creationState, SqlAstCreationState sqlAstCreationState) {
    final FromClauseAccess fromClauseAccess = sqlAstCreationState.getFromClauseAccess();
    return fromClauseAccess.resolveTableGroup(fetchablePath, p -> {
        final TableGroup lhsTableGroup = fromClauseAccess.getTableGroup(fetchParent.getNavigablePath());
        final TableGroupJoin tableGroupJoin = createTableGroupJoin(fetchablePath, lhsTableGroup, null, SqlAstJoinType.LEFT, true, false, creationState.getSqlAstCreationState());
        lhsTableGroup.addTableGroupJoin(tableGroupJoin);
        return tableGroupJoin.getJoinedGroup();
    });
}
Also used : TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) CollectionTableGroup(org.hibernate.sql.ast.tree.from.CollectionTableGroup) OneToManyTableGroup(org.hibernate.sql.ast.tree.from.OneToManyTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess)

Example 13 with FromClauseAccess

use of org.hibernate.sql.ast.spi.FromClauseAccess in project hibernate-orm by hibernate.

the class AnyDiscriminatorPart method generateFetch.

@Override
public Fetch generateFetch(FetchParent fetchParent, NavigablePath fetchablePath, FetchTiming fetchTiming, boolean selected, String resultVariable, DomainResultCreationState creationState) {
    final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
    final SessionFactoryImplementor sessionFactory = sqlAstCreationState.getCreationContext().getSessionFactory();
    final FromClauseAccess fromClauseAccess = sqlAstCreationState.getFromClauseAccess();
    final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
    final TableGroup tableGroup = fromClauseAccess.getTableGroup(fetchablePath.getParent().getParent());
    final TableReference tableReference = tableGroup.resolveTableReference(fetchablePath, table);
    final Expression columnReference = sqlExpressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, column), processingState -> new ColumnReference(tableReference, column, false, null, null, jdbcMapping(), sessionFactory));
    final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(columnReference, jdbcMapping().getMappedJavaType(), fetchParent, sessionFactory.getTypeConfiguration());
    return new BasicFetch<>(sqlSelection.getValuesArrayPosition(), fetchParent, fetchablePath, this, null, fetchTiming, creationState);
}
Also used : BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) TableReference(org.hibernate.sql.ast.tree.from.TableReference) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Example 14 with FromClauseAccess

use of org.hibernate.sql.ast.spi.FromClauseAccess in project hibernate-orm by hibernate.

the class AnyKeyPart method generateFetch.

@Override
public Fetch generateFetch(FetchParent fetchParent, NavigablePath fetchablePath, FetchTiming fetchTiming, boolean selected, String resultVariable, DomainResultCreationState creationState) {
    final FromClauseAccess fromClauseAccess = creationState.getSqlAstCreationState().getFromClauseAccess();
    final SqlExpressionResolver sqlExpressionResolver = creationState.getSqlAstCreationState().getSqlExpressionResolver();
    final SessionFactoryImplementor sessionFactory = creationState.getSqlAstCreationState().getCreationContext().getSessionFactory();
    final TableGroup tableGroup = fromClauseAccess.getTableGroup(fetchParent.getNavigablePath().getParent());
    final TableReference tableReference = tableGroup.resolveTableReference(fetchablePath, table);
    final Expression columnReference = sqlExpressionResolver.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, column), processingState -> new ColumnReference(tableReference, column, false, null, null, jdbcMapping, sessionFactory));
    final SqlSelection sqlSelection = sqlExpressionResolver.resolveSqlSelection(columnReference, getJavaType(), fetchParent, sessionFactory.getTypeConfiguration());
    return new BasicFetch<>(sqlSelection.getValuesArrayPosition(), fetchParent, fetchablePath, this, null, fetchTiming, creationState);
}
Also used : BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) TableReference(org.hibernate.sql.ast.tree.from.TableReference) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Expression(org.hibernate.sql.ast.tree.expression.Expression) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Example 15 with FromClauseAccess

use of org.hibernate.sql.ast.spi.FromClauseAccess in project hibernate-orm by hibernate.

the class ToOneAttributeMapping method determineTableGroup.

private TableGroup determineTableGroup(NavigablePath fetchablePath, FetchParent fetchParent, TableGroup parentTableGroup, String resultVariable, FromClauseAccess fromClauseAccess, DomainResultCreationState creationState) {
    final TableGroup tableGroup;
    if (fetchParent instanceof EntityResultJoinedSubclassImpl && ((EntityPersister) fetchParent.getReferencedModePart()).findDeclaredAttributeMapping(getPartName()) == null) {
        final TableGroupJoin tableGroupJoin = createTableGroupJoin(fetchablePath, parentTableGroup, resultVariable, getJoinType(fetchablePath, parentTableGroup), true, false, creationState.getSqlAstCreationState());
        parentTableGroup.addTableGroupJoin(tableGroupJoin);
        tableGroup = tableGroupJoin.getJoinedGroup();
        fromClauseAccess.registerTableGroup(fetchablePath, tableGroup);
    } else {
        tableGroup = fromClauseAccess.resolveTableGroup(fetchablePath, np -> {
            final TableGroupJoin tableGroupJoin = createTableGroupJoin(fetchablePath, parentTableGroup, resultVariable, getDefaultSqlAstJoinType(parentTableGroup), true, false, creationState.getSqlAstCreationState());
            parentTableGroup.addTableGroupJoin(tableGroupJoin);
            return tableGroupJoin.getJoinedGroup();
        });
    }
    return tableGroup;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) Arrays(java.util.Arrays) EntityPersister(org.hibernate.persister.entity.EntityPersister) Property(org.hibernate.mapping.Property) TreatedNavigablePath(org.hibernate.spi.TreatedNavigablePath) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) PropertyAccess(org.hibernate.property.access.spi.PropertyAccess) EntityResultImpl(org.hibernate.sql.results.graph.entity.internal.EntityResultImpl) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) ToOne(org.hibernate.mapping.ToOne) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) EntityAssociationMapping(org.hibernate.metamodel.mapping.EntityAssociationMapping) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) PersistentClass(org.hibernate.mapping.PersistentClass) FetchOptions(org.hibernate.sql.results.graph.FetchOptions) Join(org.hibernate.mapping.Join) TableGroupProducer(org.hibernate.sql.ast.tree.from.TableGroupProducer) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityDelayedResultImpl(org.hibernate.sql.results.graph.entity.internal.EntityDelayedResultImpl) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup) EntityValuedFetchable(org.hibernate.sql.results.graph.entity.EntityValuedFetchable) EntityDelayedFetchImpl(org.hibernate.sql.results.graph.entity.internal.EntityDelayedFetchImpl) MappedByTableGroup(org.hibernate.sql.ast.tree.from.MappedByTableGroup) TableGroupJoinProducer(org.hibernate.sql.ast.tree.from.TableGroupJoinProducer) AttributeMetadataAccess(org.hibernate.metamodel.mapping.AttributeMetadataAccess) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) OneToOne(org.hibernate.mapping.OneToOne) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart) NotFoundSnapshotResult(org.hibernate.sql.results.graph.entity.internal.NotFoundSnapshotResult) StringHelper(org.hibernate.internal.util.StringHelper) DomainResult(org.hibernate.sql.results.graph.DomainResult) Set(java.util.Set) Value(org.hibernate.mapping.Value) Collection(org.hibernate.mapping.Collection) NavigablePath(org.hibernate.spi.NavigablePath) SqlAliasBase(org.hibernate.sql.ast.spi.SqlAliasBase) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) ArrayHelper(org.hibernate.internal.util.collections.ArrayHelper) EntityIdentifierNavigablePath(org.hibernate.spi.EntityIdentifierNavigablePath) CircularBiDirectionalFetchImpl(org.hibernate.sql.results.internal.domain.CircularBiDirectionalFetchImpl) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) SelectableConsumer(org.hibernate.metamodel.mapping.SelectableConsumer) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) IndexedConsumer(org.hibernate.mapping.IndexedConsumer) FetchParent(org.hibernate.sql.results.graph.FetchParent) CircularFetchImpl(org.hibernate.sql.results.internal.domain.CircularFetchImpl) DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) SqlAliasBaseGenerator(org.hibernate.sql.ast.spi.SqlAliasBaseGenerator) EntityFetchSelectImpl(org.hibernate.sql.results.graph.entity.internal.EntityFetchSelectImpl) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) EntityResultJoinedSubclassImpl(org.hibernate.sql.results.graph.entity.internal.EntityResultJoinedSubclassImpl) SqlAliasStemHelper(org.hibernate.sql.ast.spi.SqlAliasStemHelper) EmbeddedComponentType(org.hibernate.type.EmbeddedComponentType) EntityType(org.hibernate.type.EntityType) Clause(org.hibernate.sql.ast.Clause) Supplier(java.util.function.Supplier) Objects.requireNonNullElse(java.util.Objects.requireNonNullElse) MappingType(org.hibernate.metamodel.mapping.MappingType) TableReference(org.hibernate.sql.ast.tree.from.TableReference) HashSet(java.util.HashSet) ModelPart(org.hibernate.metamodel.mapping.ModelPart) ComponentType(org.hibernate.type.ComponentType) CompositeType(org.hibernate.type.CompositeType) BiConsumer(java.util.function.BiConsumer) ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) StandardTableGroup(org.hibernate.sql.ast.tree.from.StandardTableGroup) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FetchTiming(org.hibernate.engine.FetchTiming) ManyToOne(org.hibernate.mapping.ManyToOne) LockMode(org.hibernate.LockMode) AssociationKey(org.hibernate.metamodel.mapping.AssociationKey) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) Iterator(java.util.Iterator) ModelPartContainer(org.hibernate.metamodel.mapping.ModelPartContainer) EmbeddableValuedFetchable(org.hibernate.sql.results.graph.embeddable.EmbeddableValuedFetchable) Fetch(org.hibernate.sql.results.graph.Fetch) Consumer(java.util.function.Consumer) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) EntityFetch(org.hibernate.sql.results.graph.entity.EntityFetch) FetchStyle(org.hibernate.engine.FetchStyle) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) SqlAstJoinType(org.hibernate.sql.ast.SqlAstJoinType) NotFoundAction(org.hibernate.annotations.NotFoundAction) Selectable(org.hibernate.mapping.Selectable) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) Collections(java.util.Collections) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) EntityFetchJoinedImpl(org.hibernate.sql.results.graph.entity.internal.EntityFetchJoinedImpl) Type(org.hibernate.type.Type) 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) EntityResultJoinedSubclassImpl(org.hibernate.sql.results.graph.entity.internal.EntityResultJoinedSubclassImpl)

Aggregations

FromClauseAccess (org.hibernate.sql.ast.spi.FromClauseAccess)16 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)15 NavigablePath (org.hibernate.spi.NavigablePath)9 CorrelatedTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedTableGroup)9 LazyTableGroup (org.hibernate.sql.ast.tree.from.LazyTableGroup)9 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)9 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 SqlSelection (org.hibernate.sql.ast.spi.SqlSelection)6 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)6 Expression (org.hibernate.sql.ast.tree.expression.Expression)6 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)5 SqlAstCreationState (org.hibernate.sql.ast.spi.SqlAstCreationState)5 CollectionPart (org.hibernate.metamodel.mapping.CollectionPart)4 ModelPart (org.hibernate.metamodel.mapping.ModelPart)4 SqmQuerySpec (org.hibernate.query.sqm.tree.select.SqmQuerySpec)4 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)4 CorrelatedPluralTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup)4 QueryPartTableGroup (org.hibernate.sql.ast.tree.from.QueryPartTableGroup)4 SyntheticVirtualTableGroup (org.hibernate.sql.ast.tree.from.SyntheticVirtualTableGroup)4 TableReference (org.hibernate.sql.ast.tree.from.TableReference)4