Search in sources :

Example 1 with TableGroupJoinProducer

use of org.hibernate.sql.ast.tree.from.TableGroupJoinProducer in project hibernate-orm by hibernate.

the class PluralAttributeMappingImpl method createCollectionTableGroup.

private TableGroup createCollectionTableGroup(boolean canUseInnerJoins, NavigablePath navigablePath, boolean fetched, String sourceAlias, SqlAliasBase sqlAliasBase, SqlExpressionResolver sqlExpressionResolver, FromClauseAccess fromClauseAccess, SqlAstCreationContext creationContext) {
    assert !getCollectionDescriptor().isOneToMany();
    final String collectionTableName = ((Joinable) collectionDescriptor).getTableName();
    final TableReference collectionTableReference = new NamedTableReference(collectionTableName, sqlAliasBase.generateNewAlias(), true, creationContext.getSessionFactory());
    final CollectionTableGroup tableGroup = new CollectionTableGroup(canUseInnerJoins, navigablePath, this, fetched, sourceAlias, collectionTableReference, true, sqlAliasBase, s -> false, null, creationContext.getSessionFactory());
    if (elementDescriptor instanceof TableGroupJoinProducer) {
        final TableGroupJoin tableGroupJoin = ((TableGroupJoinProducer) elementDescriptor).createTableGroupJoin(navigablePath.append(CollectionPart.Nature.ELEMENT.getName()), tableGroup, null, SqlAstJoinType.INNER, fetched, false, stem -> sqlAliasBase, sqlExpressionResolver, fromClauseAccess, creationContext);
        tableGroup.registerElementTableGroup(tableGroupJoin);
    }
    if (indexDescriptor instanceof TableGroupJoinProducer) {
        final TableGroupJoin tableGroupJoin = ((TableGroupJoinProducer) indexDescriptor).createTableGroupJoin(navigablePath.append(CollectionPart.Nature.INDEX.getName()), tableGroup, null, SqlAstJoinType.INNER, fetched, false, stem -> sqlAliasBase, sqlExpressionResolver, fromClauseAccess, creationContext);
        tableGroup.registerIndexTableGroup(tableGroupJoin);
    }
    return tableGroup;
}
Also used : TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) TableReference(org.hibernate.sql.ast.tree.from.TableReference) NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) TableGroupJoinProducer(org.hibernate.sql.ast.tree.from.TableGroupJoinProducer) Joinable(org.hibernate.persister.entity.Joinable) CollectionTableGroup(org.hibernate.sql.ast.tree.from.CollectionTableGroup)

Example 2 with TableGroupJoinProducer

use of org.hibernate.sql.ast.tree.from.TableGroupJoinProducer in project hibernate-orm by hibernate.

the class PluralAttributeMappingImpl method createOneToManyTableGroup.

private TableGroup createOneToManyTableGroup(boolean canUseInnerJoins, NavigablePath navigablePath, boolean fetched, String sourceAlias, SqlAliasBase sqlAliasBase, SqlExpressionResolver sqlExpressionResolver, FromClauseAccess fromClauseAccess, SqlAstCreationContext creationContext) {
    final TableGroup elementTableGroup = ((EntityCollectionPart) elementDescriptor).createTableGroupInternal(canUseInnerJoins, navigablePath.append(CollectionPart.Nature.ELEMENT.getName()), fetched, sourceAlias, sqlAliasBase, sqlExpressionResolver, creationContext);
    final OneToManyTableGroup tableGroup = new OneToManyTableGroup(this, elementTableGroup, creationContext.getSessionFactory());
    if (indexDescriptor instanceof TableGroupJoinProducer) {
        final TableGroupJoin tableGroupJoin = ((TableGroupJoinProducer) indexDescriptor).createTableGroupJoin(navigablePath.append(CollectionPart.Nature.INDEX.getName()), tableGroup, null, SqlAstJoinType.INNER, fetched, false, stem -> sqlAliasBase, sqlExpressionResolver, fromClauseAccess, creationContext);
        tableGroup.registerIndexTableGroup(tableGroupJoin);
    }
    return tableGroup;
}
Also used : TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) OneToManyTableGroup(org.hibernate.sql.ast.tree.from.OneToManyTableGroup) CollectionTableGroup(org.hibernate.sql.ast.tree.from.CollectionTableGroup) OneToManyTableGroup(org.hibernate.sql.ast.tree.from.OneToManyTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) TableGroupJoinProducer(org.hibernate.sql.ast.tree.from.TableGroupJoinProducer)

Example 3 with TableGroupJoinProducer

use of org.hibernate.sql.ast.tree.from.TableGroupJoinProducer in project hibernate-orm by hibernate.

the class LoaderSelectBuilder method generateSelect.

private SelectStatement generateSelect() {
    if (loadable instanceof PluralAttributeMapping) {
        final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) loadable;
        if (pluralAttributeMapping.getMappedType().getCollectionSemantics() instanceof BagSemantics) {
            currentBagRole = pluralAttributeMapping.getNavigableRole().getNavigableName();
        }
    }
    final NavigablePath rootNavigablePath = new NavigablePath(loadable.getRootPathName());
    final QuerySpec rootQuerySpec = new QuerySpec(true);
    final List<DomainResult<?>> domainResults;
    final LoaderSqlAstCreationState sqlAstCreationState = new LoaderSqlAstCreationState(rootQuerySpec, new SqlAliasBaseManager(), new SimpleFromClauseAccessImpl(), lockOptions, this::visitFetches, forceIdentifierSelection, creationContext);
    final TableGroup rootTableGroup = loadable.createRootTableGroup(true, rootNavigablePath, null, () -> rootQuerySpec::applyPredicate, sqlAstCreationState, creationContext);
    rootQuerySpec.getFromClause().addRoot(rootTableGroup);
    sqlAstCreationState.getFromClauseAccess().registerTableGroup(rootNavigablePath, rootTableGroup);
    registerPluralTableGroupParts(sqlAstCreationState.getFromClauseAccess(), rootTableGroup);
    if (partsToSelect != null && !partsToSelect.isEmpty()) {
        domainResults = new ArrayList<>(partsToSelect.size());
        for (ModelPart part : partsToSelect) {
            final NavigablePath navigablePath = rootNavigablePath.append(part.getPartName());
            final TableGroup tableGroup;
            if (part instanceof TableGroupJoinProducer) {
                final TableGroupJoinProducer tableGroupJoinProducer = (TableGroupJoinProducer) part;
                final TableGroupJoin tableGroupJoin = tableGroupJoinProducer.createTableGroupJoin(navigablePath, rootTableGroup, null, SqlAstJoinType.LEFT, true, false, sqlAstCreationState);
                rootTableGroup.addTableGroupJoin(tableGroupJoin);
                tableGroup = tableGroupJoin.getJoinedGroup();
                sqlAstCreationState.getFromClauseAccess().registerTableGroup(navigablePath, tableGroup);
                registerPluralTableGroupParts(sqlAstCreationState.getFromClauseAccess(), tableGroup);
            } else {
                tableGroup = rootTableGroup;
            }
            domainResults.add(part.createDomainResult(navigablePath, tableGroup, null, sqlAstCreationState));
        }
    } else {
        // use the one passed to the constructor or create one (maybe always create and pass?)
        // allows re-use as they can be re-used to save on memory - they
        // do not share state between
        // noinspection rawtypes
        final DomainResult domainResult;
        if (this.cachedDomainResult != null) {
            // used the one passed to the constructor
            domainResult = this.cachedDomainResult;
        } else {
            // create one
            domainResult = loadable.createDomainResult(rootNavigablePath, rootTableGroup, null, sqlAstCreationState);
        }
        // noinspection unchecked
        domainResults = Collections.singletonList(domainResult);
    }
    for (ModelPart restrictedPart : restrictedParts) {
        final int numberOfRestrictionColumns = restrictedPart.getJdbcTypeCount();
        applyRestriction(rootQuerySpec, rootNavigablePath, rootTableGroup, restrictedPart, numberOfRestrictionColumns, jdbcParameterConsumer, sqlAstCreationState);
    }
    if (loadable instanceof PluralAttributeMapping) {
        final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) loadable;
        applyFiltering(rootQuerySpec, rootTableGroup, pluralAttributeMapping, sqlAstCreationState);
        applyOrdering(rootTableGroup, pluralAttributeMapping);
    } else {
        applyFiltering(rootQuerySpec, rootTableGroup, (Restrictable) loadable, sqlAstCreationState);
    }
    if (orderByFragments != null) {
        orderByFragments.forEach(entry -> entry.getKey().apply(rootQuerySpec, entry.getValue(), sqlAstCreationState));
    }
    return new SelectStatement(rootQuerySpec, domainResults);
}
Also used : NavigablePath(org.hibernate.query.spi.NavigablePath) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) TableGroupJoinProducer(org.hibernate.sql.ast.tree.from.TableGroupJoinProducer) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) ModelPart(org.hibernate.metamodel.mapping.ModelPart) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) CollectionDomainResult(org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult) DomainResult(org.hibernate.sql.results.graph.DomainResult) SimpleFromClauseAccessImpl(org.hibernate.sql.ast.spi.SimpleFromClauseAccessImpl) SqlAliasBaseManager(org.hibernate.sql.ast.spi.SqlAliasBaseManager) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) BagSemantics(org.hibernate.collection.spi.BagSemantics) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec)

Example 4 with TableGroupJoinProducer

use of org.hibernate.sql.ast.tree.from.TableGroupJoinProducer in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method consumeAttributeJoin.

private TableGroup consumeAttributeJoin(SqmAttributeJoin<?, ?> sqmJoin, TableGroup lhsTableGroup, TableGroup ownerTableGroup, boolean transitive) {
    final SqmPathSource<?> pathSource = sqmJoin.getReferencedPathSource();
    final SqmJoinType sqmJoinType = sqmJoin.getSqmJoinType();
    final TableGroupJoin joinedTableGroupJoin;
    final TableGroup joinedTableGroup;
    final NavigablePath sqmJoinNavigablePath = sqmJoin.getNavigablePath();
    final ModelPart modelPart = ownerTableGroup.getModelPart().findSubPart(pathSource.getPathName(), SqmMappingModelHelper.resolveExplicitTreatTarget(sqmJoin, this));
    if (pathSource instanceof PluralPersistentAttribute) {
        assert modelPart instanceof PluralAttributeMapping;
        final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) modelPart;
        if (sqmJoin.isFetched()) {
            containsCollectionFetches = true;
        }
        joinedTableGroupJoin = pluralAttributeMapping.createTableGroupJoin(sqmJoinNavigablePath, ownerTableGroup, sqmJoin.getExplicitAlias(), sqmJoinType.getCorrespondingSqlJoinType(), sqmJoin.isFetched(), sqmJoin.getJoinPredicate() != null, this);
        joinedTableGroup = joinedTableGroupJoin.getJoinedGroup();
        pluralAttributeMapping.applyBaseRestrictions((predicate) -> {
            final PredicateCollector existing = collectionFilterPredicates.get(joinedTableGroup.getGroupAlias());
            final PredicateCollector collector;
            if (existing == null) {
                collector = new PredicateCollector(predicate);
                collectionFilterPredicates.put(joinedTableGroup.getGroupAlias(), collector);
            } else {
                collector = existing;
                collector.applyPredicate(predicate);
            }
        }, joinedTableGroup, true, getLoadQueryInfluencers().getEnabledFilters(), null, this);
    } else {
        assert modelPart instanceof TableGroupJoinProducer;
        joinedTableGroupJoin = ((TableGroupJoinProducer) modelPart).createTableGroupJoin(sqmJoinNavigablePath, ownerTableGroup, sqmJoin.getExplicitAlias(), sqmJoinType.getCorrespondingSqlJoinType(), sqmJoin.isFetched(), sqmJoin.getJoinPredicate() != null, this);
        joinedTableGroup = joinedTableGroupJoin.getJoinedGroup();
        // Left or inner singular attribute joins without a predicate can be safely optimized away
        if (sqmJoin.getJoinPredicate() != null || sqmJoinType != SqmJoinType.INNER && sqmJoinType != SqmJoinType.LEFT) {
            joinedTableGroup.getPrimaryTableReference();
        }
    }
    lhsTableGroup.addTableGroupJoin(joinedTableGroupJoin);
    getFromClauseIndex().register(sqmJoin, joinedTableGroup);
    registerPluralTableGroupParts(joinedTableGroup);
    // For joins we also need to register the table groups for the treats
    if (joinedTableGroup instanceof PluralTableGroup) {
        final PluralTableGroup pluralTableGroup = (PluralTableGroup) joinedTableGroup;
        for (SqmFrom<?, ?> sqmTreat : sqmJoin.getSqmTreats()) {
            if (pluralTableGroup.getElementTableGroup() != null) {
                getFromClauseAccess().registerTableGroup(sqmTreat.getNavigablePath().append(CollectionPart.Nature.ELEMENT.getName()), pluralTableGroup.getElementTableGroup());
            }
            if (pluralTableGroup.getIndexTableGroup() != null) {
                getFromClauseAccess().registerTableGroup(sqmTreat.getNavigablePath().append(CollectionPart.Nature.INDEX.getName()), pluralTableGroup.getIndexTableGroup());
            }
        }
    } else {
        for (SqmFrom<?, ?> sqmTreat : sqmJoin.getSqmTreats()) {
            getFromClauseAccess().registerTableGroup(sqmTreat.getNavigablePath(), joinedTableGroup);
        }
    }
    // add any additional join restrictions
    if (sqmJoin.getJoinPredicate() != null) {
        if (sqmJoin.isFetched()) {
            QueryLogging.QUERY_MESSAGE_LOGGER.debugf("Join fetch [" + sqmJoinNavigablePath + "] is restricted");
        }
        final SqmJoin<?, ?> oldJoin = currentlyProcessingJoin;
        currentlyProcessingJoin = sqmJoin;
        joinedTableGroupJoin.applyPredicate(visitNestedTopLevelPredicate(sqmJoin.getJoinPredicate()));
        currentlyProcessingJoin = oldJoin;
    }
    if (transitive) {
        consumeExplicitJoins(sqmJoin, joinedTableGroup);
    }
    return joinedTableGroup;
}
Also used : VirtualTableGroup(org.hibernate.sql.ast.tree.from.VirtualTableGroup) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup) CorrelatedPluralTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) QueryPartTableGroup(org.hibernate.sql.ast.tree.from.QueryPartTableGroup) NavigablePath(org.hibernate.query.spi.NavigablePath) TableGroupJoinProducer(org.hibernate.sql.ast.tree.from.TableGroupJoinProducer) ConvertibleModelPart(org.hibernate.metamodel.mapping.ConvertibleModelPart) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) PluralPersistentAttribute(org.hibernate.metamodel.model.domain.PluralPersistentAttribute) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) CorrelatedPluralTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) SqmJoinType(org.hibernate.query.sqm.tree.SqmJoinType) PredicateCollector(org.hibernate.sql.ast.tree.predicate.PredicateCollector)

Example 5 with TableGroupJoinProducer

use of org.hibernate.sql.ast.tree.from.TableGroupJoinProducer in project hibernate-orm by hibernate.

the class DynamicFetchBuilderLegacy method buildFetch.

@Override
public Fetch buildFetch(FetchParent parent, NavigablePath fetchPath, JdbcValuesMetadata jdbcResultsMetadata, BiFunction<String, String, DynamicFetchBuilderLegacy> legacyFetchResolver, DomainResultCreationState domainResultCreationState) {
    final DomainResultCreationStateImpl creationState = ResultsHelper.impl(domainResultCreationState);
    final TableGroup ownerTableGroup = creationState.getFromClauseAccess().findByAlias(ownerTableAlias);
    final AttributeMapping attributeMapping = parent.getReferencedMappingContainer().findContainingEntityMapping().findDeclaredAttributeMapping(fetchableName);
    final TableGroup tableGroup;
    if (attributeMapping instanceof TableGroupJoinProducer) {
        final SqlAliasBase sqlAliasBase = new SqlAliasBaseConstant(tableAlias);
        final TableGroupJoin tableGroupJoin = ((TableGroupJoinProducer) attributeMapping).createTableGroupJoin(fetchPath, ownerTableGroup, tableAlias, SqlAstJoinType.INNER, true, false, s -> sqlAliasBase, creationState.getSqlExpressionResolver(), creationState.getFromClauseAccess(), creationState.getCreationContext());
        ownerTableGroup.addTableGroupJoin(tableGroupJoin);
        creationState.getFromClauseAccess().registerTableGroup(fetchPath, tableGroup = tableGroupJoin.getJoinedGroup());
    } else {
        tableGroup = ownerTableGroup;
    }
    if (columnNames != null) {
        final ForeignKeyDescriptor keyDescriptor;
        if (attributeMapping instanceof PluralAttributeMapping) {
            final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) attributeMapping;
            keyDescriptor = pluralAttributeMapping.getKeyDescriptor();
        } else {
            // Not sure if this fetch builder can also be used with other attribute mappings
            assert attributeMapping instanceof ToOneAttributeMapping;
            final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
            keyDescriptor = toOneAttributeMapping.getForeignKeyDescriptor();
        }
        if (!columnNames.isEmpty()) {
            keyDescriptor.forEachSelectable((selectionIndex, selectableMapping) -> {
                resolveSqlSelection(columnNames.get(selectionIndex), createColumnReferenceKey(tableGroup.resolveTableReference(selectableMapping.getContainingTableExpression()), selectableMapping.getSelectionExpression()), selectableMapping.getJdbcMapping(), jdbcResultsMetadata, domainResultCreationState);
            });
        }
        // We process the fetch builder such that it contains a resultBuilderEntity before calling this method in ResultSetMappingProcessor
        if (resultBuilderEntity != null) {
            return resultBuilderEntity.buildFetch(parent, attributeMapping, jdbcResultsMetadata, creationState);
        }
    }
    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)) {
                return findFetchBuilder(relativePath.substring(prefix.length()));
            }
            return null;
        });
        return parent.generateFetchableFetch(attributeMapping, parent.resolveNavigablePath(attributeMapping), FetchTiming.IMMEDIATE, true, null, domainResultCreationState);
    } finally {
        creationState.popExplicitFetchMementoResolver();
    }
}
Also used : TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) DomainResultCreationStateImpl(org.hibernate.query.results.DomainResultCreationStateImpl) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) TableGroupJoinProducer(org.hibernate.sql.ast.tree.from.TableGroupJoinProducer) NavigablePath(org.hibernate.query.spi.NavigablePath) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) SqlAliasBase(org.hibernate.sql.ast.spi.SqlAliasBase)

Aggregations

TableGroupJoin (org.hibernate.sql.ast.tree.from.TableGroupJoin)7 TableGroupJoinProducer (org.hibernate.sql.ast.tree.from.TableGroupJoinProducer)7 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)6 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)4 NavigablePath (org.hibernate.query.spi.NavigablePath)4 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)4 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)3 EntityValuedModelPart (org.hibernate.metamodel.mapping.EntityValuedModelPart)3 ModelPart (org.hibernate.metamodel.mapping.ModelPart)3 CorrelatedPluralTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup)3 CorrelatedTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedTableGroup)3 LazyTableGroup (org.hibernate.sql.ast.tree.from.LazyTableGroup)3 QueryPartTableGroup (org.hibernate.sql.ast.tree.from.QueryPartTableGroup)3 VirtualTableGroup (org.hibernate.sql.ast.tree.from.VirtualTableGroup)3 ConvertibleModelPart (org.hibernate.metamodel.mapping.ConvertibleModelPart)2 EmbeddableValuedModelPart (org.hibernate.metamodel.mapping.EmbeddableValuedModelPart)2 CollectionTableGroup (org.hibernate.sql.ast.tree.from.CollectionTableGroup)2 QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)2 BigInteger (java.math.BigInteger)1 BagSemantics (org.hibernate.collection.spi.BagSemantics)1