Search in sources :

Example 1 with SqmCorrelation

use of org.hibernate.query.sqm.tree.domain.SqmCorrelation in project hibernate-orm by hibernate.

the class SqmSubQuery method getCorrelatedJoins.

@Override
public Set<Join<?, ?>> getCorrelatedJoins() {
    final Set<Join<?, ?>> correlatedJoins = new HashSet<>();
    final SqmFromClause fromClause = getQuerySpec().getFromClause();
    if (fromClause == null) {
        return correlatedJoins;
    }
    for (SqmRoot<?> root : fromClause.getRoots()) {
        if (root instanceof SqmCorrelation<?, ?>) {
            for (SqmJoin<?, ?> sqmJoin : root.getSqmJoins()) {
                if (sqmJoin instanceof SqmCorrelation<?, ?> && sqmJoin instanceof Join<?, ?>) {
                    correlatedJoins.add((Join<?, ?>) sqmJoin);
                }
            }
        }
    }
    return correlatedJoins;
}
Also used : SqmCorrelation(org.hibernate.query.sqm.tree.domain.SqmCorrelation) SqmAttributeJoin(org.hibernate.query.sqm.tree.from.SqmAttributeJoin) SqmCorrelatedMapJoin(org.hibernate.query.sqm.tree.domain.SqmCorrelatedMapJoin) SqmEntityJoin(org.hibernate.query.sqm.tree.from.SqmEntityJoin) CollectionJoin(jakarta.persistence.criteria.CollectionJoin) SqmCorrelatedSetJoin(org.hibernate.query.sqm.tree.domain.SqmCorrelatedSetJoin) SqmMapJoin(org.hibernate.query.sqm.tree.domain.SqmMapJoin) SqmSingularJoin(org.hibernate.query.sqm.tree.domain.SqmSingularJoin) SetJoin(jakarta.persistence.criteria.SetJoin) MapJoin(jakarta.persistence.criteria.MapJoin) SqmJoin(org.hibernate.query.sqm.tree.from.SqmJoin) SqmCorrelatedBagJoin(org.hibernate.query.sqm.tree.domain.SqmCorrelatedBagJoin) SqmCorrelatedSingularJoin(org.hibernate.query.sqm.tree.domain.SqmCorrelatedSingularJoin) SqmCrossJoin(org.hibernate.query.sqm.tree.from.SqmCrossJoin) SqmCorrelatedListJoin(org.hibernate.query.sqm.tree.domain.SqmCorrelatedListJoin) PluralJoin(jakarta.persistence.criteria.PluralJoin) SqmListJoin(org.hibernate.query.sqm.tree.domain.SqmListJoin) SqmSetJoin(org.hibernate.query.sqm.tree.domain.SqmSetJoin) ListJoin(jakarta.persistence.criteria.ListJoin) SqmCorrelatedEntityJoin(org.hibernate.query.sqm.tree.domain.SqmCorrelatedEntityJoin) SqmBagJoin(org.hibernate.query.sqm.tree.domain.SqmBagJoin) SqmCorrelatedCrossJoin(org.hibernate.query.sqm.tree.domain.SqmCorrelatedCrossJoin) Join(jakarta.persistence.criteria.Join) SqmFromClause(org.hibernate.query.sqm.tree.from.SqmFromClause) HashSet(java.util.HashSet)

Example 2 with SqmCorrelation

use of org.hibernate.query.sqm.tree.domain.SqmCorrelation in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method consumeJoins.

private void consumeJoins(SqmRoot<?> sqmRoot, FromClauseIndex fromClauseIndex, TableGroup tableGroup) {
    if (sqmRoot.getOrderedJoins() == null) {
        consumeExplicitJoins(sqmRoot, tableGroup);
    } else {
        if (log.isTraceEnabled()) {
            log.tracef("Visiting explicit joins for `%s`", sqmRoot.getNavigablePath());
        }
        TableGroup lastTableGroup = tableGroup;
        for (SqmJoin<?, ?> join : sqmRoot.getOrderedJoins()) {
            final TableGroup ownerTableGroup;
            if (join.getLhs() == null) {
                ownerTableGroup = tableGroup;
            } else {
                if (join.getLhs() instanceof SqmCorrelation<?, ?>) {
                    ownerTableGroup = fromClauseIndex.findTableGroup(((SqmCorrelation<?, ?>) join.getLhs()).getCorrelatedRoot().getNavigablePath());
                } else {
                    ownerTableGroup = fromClauseIndex.findTableGroup(join.getLhs().getNavigablePath());
                }
            }
            assert ownerTableGroup != null;
            final TableGroup actualTableGroup = findActualTableGroup(ownerTableGroup, join);
            lastTableGroup = consumeExplicitJoin(join, lastTableGroup, actualTableGroup, false);
        }
    }
}
Also used : SqmCorrelation(org.hibernate.query.sqm.tree.domain.SqmCorrelation) 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)

Example 3 with SqmCorrelation

use of org.hibernate.query.sqm.tree.domain.SqmCorrelation in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitEntityWithJoins.

@Override
public SqmRoot<?> visitEntityWithJoins(HqlParser.EntityWithJoinsContext parserSpace) {
    final SqmRoot<?> sqmRoot = visitRootEntity((HqlParser.RootEntityContext) parserSpace.getChild(0));
    final SqmFromClause fromClause = currentQuerySpec().getFromClause();
    // Correlations are implicitly added to the from clause
    if (!(sqmRoot instanceof SqmCorrelation<?, ?>)) {
        fromClause.addRoot(sqmRoot);
    }
    final int size = parserSpace.getChildCount();
    for (int i = 1; i < size; i++) {
        final ParseTree parseTree = parserSpace.getChild(i);
        if (parseTree instanceof HqlParser.CrossJoinContext) {
            consumeCrossJoin((HqlParser.CrossJoinContext) parseTree, sqmRoot);
        } else if (parseTree instanceof HqlParser.JoinContext) {
            consumeJoin((HqlParser.JoinContext) parseTree, sqmRoot);
        } else if (parseTree instanceof HqlParser.JpaCollectionJoinContext) {
            consumeJpaCollectionJoin((HqlParser.JpaCollectionJoinContext) parseTree, sqmRoot);
        }
    }
    return sqmRoot;
}
Also used : SqmCorrelation(org.hibernate.query.sqm.tree.domain.SqmCorrelation) HqlParser(org.hibernate.grammars.hql.HqlParser) SqmFromClause(org.hibernate.query.sqm.tree.from.SqmFromClause) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 4 with SqmCorrelation

use of org.hibernate.query.sqm.tree.domain.SqmCorrelation in project hibernate-orm by hibernate.

the class SemanticQueryBuilder method visitRootEntity.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public SqmRoot<?> visitRootEntity(HqlParser.RootEntityContext ctx) {
    final HqlParser.EntityNameContext entityNameContext = (HqlParser.EntityNameContext) ctx.getChild(0);
    final List<ParseTree> entityNameParseTreeChildren = entityNameContext.children;
    final String name = getEntityName(entityNameContext);
    log.debugf("Handling root path - %s", name);
    final EntityDomainType entityDescriptor = getCreationContext().getJpaMetamodel().getHqlEntityReference(name);
    final HqlParser.VariableContext identificationVariableDefContext;
    if (ctx.getChildCount() > 1) {
        identificationVariableDefContext = (HqlParser.VariableContext) ctx.getChild(1);
    } else {
        identificationVariableDefContext = null;
    }
    final String alias = applyJpaCompliance(visitVariable(identificationVariableDefContext));
    final SqmCreationProcessingState processingState = processingStateStack.getCurrent();
    final SqmPathRegistry pathRegistry = processingState.getPathRegistry();
    if (entityDescriptor == null) {
        final int size = entityNameParseTreeChildren.size();
        // Handle the use of a correlation path in subqueries
        if (processingStateStack.depth() > 1 && size > 2) {
            final String parentAlias = entityNameParseTreeChildren.get(0).getText();
            final AbstractSqmFrom<?, ?> correlation = processingState.getPathRegistry().findFromByAlias(parentAlias, true);
            if (correlation instanceof SqmCorrelation<?, ?>) {
                final DotIdentifierConsumer dotIdentifierConsumer = new QualifiedJoinPathConsumer(correlation, SqmJoinType.INNER, false, alias, this);
                final int lastIdx = size - 1;
                for (int i = 2; i != lastIdx; i += 2) {
                    dotIdentifierConsumer.consumeIdentifier(entityNameParseTreeChildren.get(i).getText(), false, false);
                }
                dotIdentifierConsumer.consumeIdentifier(entityNameParseTreeChildren.get(lastIdx).getText(), false, true);
                return ((SqmCorrelation<?, ?>) correlation).getCorrelatedRoot();
            }
            throw new SemanticException("Could not resolve entity or correlation path '" + name + "'");
        }
        throw new UnknownEntityException("Could not resolve root entity '" + name + "'", name);
    }
    checkFQNEntityNameJpaComplianceViolationIfNeeded(name, entityDescriptor);
    if (entityDescriptor instanceof SqmPolymorphicRootDescriptor) {
        if (getCreationOptions().useStrictJpaCompliance()) {
            throw new StrictJpaComplianceViolation("Encountered unmapped polymorphic reference [" + entityDescriptor.getHibernateEntityName() + "], but strict JPQL compliance was requested", StrictJpaComplianceViolation.Type.UNMAPPED_POLYMORPHISM);
        }
        if (processingStateStack.depth() > 1) {
            throw new SemanticException("Illegal implicit-polymorphic domain path in subquery '" + entityDescriptor.getName() + "'");
        }
    }
    final SqmRoot<?> sqmRoot = new SqmRoot<>(entityDescriptor, alias, true, creationContext.getNodeBuilder());
    pathRegistry.register(sqmRoot);
    return sqmRoot;
}
Also used : UnknownEntityException(org.hibernate.query.sqm.UnknownEntityException) SqmRoot(org.hibernate.query.sqm.tree.from.SqmRoot) SqmCorrelation(org.hibernate.query.sqm.tree.domain.SqmCorrelation) SqmCreationProcessingState(org.hibernate.query.hql.spi.SqmCreationProcessingState) HqlParser(org.hibernate.grammars.hql.HqlParser) SqmPathRegistry(org.hibernate.query.hql.spi.SqmPathRegistry) StrictJpaComplianceViolation(org.hibernate.query.sqm.StrictJpaComplianceViolation) DotIdentifierConsumer(org.hibernate.query.hql.spi.DotIdentifierConsumer) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) SqmPolymorphicRootDescriptor(org.hibernate.query.sqm.tree.domain.SqmPolymorphicRootDescriptor) ParseTree(org.antlr.v4.runtime.tree.ParseTree) SemanticException(org.hibernate.query.SemanticException)

Aggregations

SqmCorrelation (org.hibernate.query.sqm.tree.domain.SqmCorrelation)4 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 HqlParser (org.hibernate.grammars.hql.HqlParser)2 SqmFromClause (org.hibernate.query.sqm.tree.from.SqmFromClause)2 CollectionJoin (jakarta.persistence.criteria.CollectionJoin)1 Join (jakarta.persistence.criteria.Join)1 ListJoin (jakarta.persistence.criteria.ListJoin)1 MapJoin (jakarta.persistence.criteria.MapJoin)1 PluralJoin (jakarta.persistence.criteria.PluralJoin)1 SetJoin (jakarta.persistence.criteria.SetJoin)1 HashSet (java.util.HashSet)1 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)1 SemanticException (org.hibernate.query.SemanticException)1 DotIdentifierConsumer (org.hibernate.query.hql.spi.DotIdentifierConsumer)1 SqmCreationProcessingState (org.hibernate.query.hql.spi.SqmCreationProcessingState)1 SqmPathRegistry (org.hibernate.query.hql.spi.SqmPathRegistry)1 StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)1 UnknownEntityException (org.hibernate.query.sqm.UnknownEntityException)1 SqmBagJoin (org.hibernate.query.sqm.tree.domain.SqmBagJoin)1 SqmCorrelatedBagJoin (org.hibernate.query.sqm.tree.domain.SqmCorrelatedBagJoin)1