Search in sources :

Example 21 with TableGroupJoin

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

the class BaseSqmToSqlAstConverter method visitMapEntryFunction.

@Override
public Object visitMapEntryFunction(SqmMapEntryReference<?, ?> entryRef) {
    final SqmPath<?> mapPath = entryRef.getMapPath();
    prepareReusablePath(mapPath, () -> null);
    final NavigablePath mapNavigablePath = mapPath.getNavigablePath();
    final TableGroup tableGroup = getFromClauseAccess().resolveTableGroup(mapNavigablePath, (navigablePath) -> {
        final TableGroup parentTableGroup = getFromClauseAccess().getTableGroup(mapNavigablePath.getParent());
        final PluralAttributeMapping mapAttribute = (PluralAttributeMapping) parentTableGroup.getModelPart().findSubPart(mapNavigablePath.getLocalName(), null);
        final TableGroupJoin tableGroupJoin = mapAttribute.createTableGroupJoin(mapNavigablePath, parentTableGroup, null, SqlAstJoinType.INNER, false, false, sqlAliasBaseManager, getSqlExpressionResolver(), this, creationContext);
        parentTableGroup.addTableGroupJoin(tableGroupJoin);
        return tableGroupJoin.getJoinedGroup();
    });
    final PluralAttributeMapping mapDescriptor = (PluralAttributeMapping) tableGroup.getModelPart();
    final CollectionPart indexDescriptor = mapDescriptor.getIndexDescriptor();
    final NavigablePath indexNavigablePath = mapNavigablePath.append(indexDescriptor.getPartName());
    final DomainResult<Object> indexResult = indexDescriptor.createDomainResult(indexNavigablePath, tableGroup, null, this);
    final CollectionPart valueDescriptor = mapDescriptor.getElementDescriptor();
    final NavigablePath valueNavigablePath = mapNavigablePath.append(valueDescriptor.getPartName());
    final DomainResult<Object> valueResult = valueDescriptor.createDomainResult(valueNavigablePath, tableGroup, null, this);
    return new DomainResultProducer<Map.Entry<Object, Object>>() {

        @Override
        public DomainResult<Map.Entry<Object, Object>> createDomainResult(String resultVariable, DomainResultCreationState creationState) {
            final JavaType<Map.Entry<Object, Object>> mapEntryDescriptor = getTypeConfiguration().getJavaTypeRegistry().resolveDescriptor(Map.Entry.class);
            return new SqmMapEntryResult<>(indexResult, valueResult, resultVariable, mapEntryDescriptor);
        }

        @Override
        public void applySqlSelections(DomainResultCreationState creationState) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : NavigablePath(org.hibernate.query.spi.NavigablePath) 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) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) DomainResultProducer(org.hibernate.query.sqm.sql.internal.DomainResultProducer) SqmMapEntryResult(org.hibernate.query.sqm.sql.internal.SqmMapEntryResult) EntityCollectionPart(org.hibernate.metamodel.mapping.internal.EntityCollectionPart) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) EmbeddedCollectionPart(org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) AbstractMap(java.util.AbstractMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap)

Example 22 with TableGroupJoin

use of org.hibernate.sql.ast.tree.from.TableGroupJoin 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 23 with TableGroupJoin

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

the class CountFunction method hasNeighbouringJoinsAlteringNullability.

private Boolean hasNeighbouringJoinsAlteringNullability(TableGroup tableGroup, TableGroup targetTableGroup) {
    if (tableGroup == targetTableGroup) {
        return Boolean.FALSE;
    }
    final List<TableGroupJoin> tableGroupJoins = tableGroup.getTableGroupJoins();
    int tableGroupIndex = -1;
    for (int i = 0; i < tableGroupJoins.size(); i++) {
        final TableGroupJoin tableGroupJoin = tableGroupJoins.get(i);
        final Boolean result = hasNeighbouringJoinsAlteringNullability(tableGroupJoin.getJoinedGroup(), targetTableGroup);
        if (result == Boolean.TRUE) {
            return Boolean.TRUE;
        } else if (result != null) {
            tableGroupIndex = i;
            break;
        }
    }
    if (tableGroupIndex != -1) {
        for (int i = 0; i < tableGroupJoins.size(); i++) {
            if (i == tableGroupIndex) {
                continue;
            }
            final TableGroupJoin tableGroupJoin = tableGroupJoins.get(i);
            if (hasJoinsAlteringNullability(tableGroupJoin)) {
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }
    return null;
}
Also used : TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin)

Example 24 with TableGroupJoin

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

the class AbstractSqlAstTranslator method renderTableGroup.

protected void renderTableGroup(TableGroup tableGroup, Predicate predicate, List<TableGroupJoin> tableGroupJoinCollector) {
    // Without reference joins or nested join groups, even a real table group does not need parenthesis
    final boolean realTableGroup = tableGroup.isRealTableGroup() && (CollectionHelper.isNotEmpty(tableGroup.getTableReferenceJoins()) || hasNestedTableGroupsToRender(tableGroup.getNestedTableGroupJoins()));
    if (realTableGroup) {
        appendSql(OPEN_PARENTHESIS);
    }
    final LockMode effectiveLockMode = getEffectiveLockMode(tableGroup.getSourceAlias());
    final boolean usesLockHint = renderPrimaryTableReference(tableGroup, effectiveLockMode);
    final List<TableGroupJoin> tableGroupJoins;
    if (realTableGroup) {
        // For real table groups, we collect all normal table group joins within that table group
        // The purpose of that is to render them in-order outside of the group/parenthesis
        // This is necessary for at least Derby but is also a lot easier to read
        renderTableReferenceJoins(tableGroup);
        if (tableGroupJoinCollector == null) {
            tableGroupJoins = new ArrayList<>();
            processNestedTableGroupJoins(tableGroup, tableGroupJoins);
        } else {
            tableGroupJoins = null;
            processNestedTableGroupJoins(tableGroup, tableGroupJoinCollector);
        }
        appendSql(CLOSE_PARENTHESIS);
    } else {
        tableGroupJoins = null;
    }
    if (predicate != null) {
        appendSql(" on ");
        predicate.accept(this);
    }
    if (tableGroup.isLateral() && !getDialect().supportsLateral()) {
        final Predicate lateralEmulationPredicate = determineLateralEmulationPredicate(tableGroup);
        if (lateralEmulationPredicate != null) {
            if (predicate == null) {
                appendSql(" on ");
            } else {
                appendSql(" and ");
            }
            lateralEmulationPredicate.accept(this);
        }
    }
    if (!realTableGroup) {
        renderTableReferenceJoins(tableGroup);
        processNestedTableGroupJoins(tableGroup, tableGroupJoinCollector);
    }
    if (tableGroupJoinCollector != null) {
        tableGroupJoinCollector.addAll(tableGroup.getTableGroupJoins());
    } else {
        if (tableGroupJoins != null) {
            for (TableGroupJoin tableGroupJoin : tableGroupJoins) {
                processTableGroupJoin(tableGroupJoin, null);
            }
        }
        processTableGroupJoins(tableGroup);
    }
    ModelPartContainer modelPart = tableGroup.getModelPart();
    if (modelPart instanceof AbstractEntityPersister) {
        String[] querySpaces = (String[]) ((AbstractEntityPersister) modelPart).getQuerySpaces();
        for (int i = 0; i < querySpaces.length; i++) {
            registerAffectedTable(querySpaces[i]);
        }
    }
    if (!usesLockHint && tableGroup.getSourceAlias() != null && LockMode.READ.lessThan(effectiveLockMode)) {
        if (forUpdate == null) {
            forUpdate = new ForUpdateClause(effectiveLockMode);
        } else {
            forUpdate.setLockMode(effectiveLockMode);
        }
        forUpdate.applyAliases(getDialect().getLockRowIdentifier(effectiveLockMode), tableGroup);
    }
}
Also used : TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) LockMode(org.hibernate.LockMode) ModelPartContainer(org.hibernate.metamodel.mapping.ModelPartContainer) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) SqlFragmentPredicate(org.hibernate.persister.internal.SqlFragmentPredicate) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SelfRenderingPredicate(org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate) FilterPredicate(org.hibernate.sql.ast.tree.predicate.FilterPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) LikePredicate(org.hibernate.sql.ast.tree.predicate.LikePredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) BetweenPredicate(org.hibernate.sql.ast.tree.predicate.BetweenPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) GroupedPredicate(org.hibernate.sql.ast.tree.predicate.GroupedPredicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) ExistsPredicate(org.hibernate.sql.ast.tree.predicate.ExistsPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate)

Example 25 with TableGroupJoin

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

the class LoaderSelectBuilder method applyFiltering.

private void applyFiltering(QuerySpec querySpec, TableGroup tableGroup, PluralAttributeMapping pluralAttributeMapping, SqlAstCreationState astCreationState) {
    pluralAttributeMapping.applyBaseRestrictions(querySpec::applyPredicate, tableGroup, true, loadQueryInfluencers.getEnabledFilters(), null, astCreationState);
    pluralAttributeMapping.applyBaseManyToManyRestrictions((filterPredicate) -> {
        final NavigablePath parentNavigablePath = tableGroup.getNavigablePath().getParent();
        if (parentNavigablePath == null) {
            querySpec.applyPredicate(filterPredicate);
        } else {
            final TableGroup parentTableGroup = astCreationState.getFromClauseAccess().getTableGroup(parentNavigablePath);
            TableGroupJoin pluralTableGroupJoin = null;
            for (TableGroupJoin nestedTableGroupJoin : parentTableGroup.getTableGroupJoins()) {
                if (nestedTableGroupJoin.getNavigablePath() == tableGroup.getNavigablePath()) {
                    pluralTableGroupJoin = nestedTableGroupJoin;
                    break;
                }
            }
            assert pluralTableGroupJoin != null;
            pluralTableGroupJoin.applyPredicate(filterPredicate);
        }
    }, tableGroup, true, loadQueryInfluencers.getEnabledFilters(), null, astCreationState);
}
Also used : TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) 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)

Aggregations

TableGroupJoin (org.hibernate.sql.ast.tree.from.TableGroupJoin)39 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)34 LazyTableGroup (org.hibernate.sql.ast.tree.from.LazyTableGroup)17 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)14 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)14 NavigablePath (org.hibernate.query.spi.NavigablePath)13 SqlAstJoinType (org.hibernate.sql.ast.SqlAstJoinType)13 EntityPersister (org.hibernate.persister.entity.EntityPersister)11 CorrelatedTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedTableGroup)11 QueryPartTableGroup (org.hibernate.sql.ast.tree.from.QueryPartTableGroup)10 TableGroupJoinProducer (org.hibernate.sql.ast.tree.from.TableGroupJoinProducer)10 VirtualTableGroup (org.hibernate.sql.ast.tree.from.VirtualTableGroup)10 Fetch (org.hibernate.sql.results.graph.Fetch)10 CorrelatedPluralTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup)9 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)9 EntityValuedModelPart (org.hibernate.metamodel.mapping.EntityValuedModelPart)8 AbstractEntityPersister (org.hibernate.persister.entity.AbstractEntityPersister)8 DomainResult (org.hibernate.sql.results.graph.DomainResult)8 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)7 ModelPart (org.hibernate.metamodel.mapping.ModelPart)7