Search in sources :

Example 1 with SqmPathRegistry

use of org.hibernate.query.hql.spi.SqmPathRegistry in project hibernate-orm by hibernate.

the class SqmPluralValuedSimplePath method resolveIndexedAccess.

@Override
public SqmPath<?> resolveIndexedAccess(SqmExpression<?> selector, boolean isTerminal, SqmCreationState creationState) {
    final SqmPathRegistry pathRegistry = creationState.getCurrentProcessingState().getPathRegistry();
    final String alias = selector.toHqlString();
    final NavigablePath navigablePath = getNavigablePath().getParent().append(getNavigablePath().getUnaliasedLocalName(), alias).append(CollectionPart.Nature.ELEMENT.getName());
    final SqmFrom<?, ?> indexedPath = pathRegistry.findFromByPath(navigablePath);
    if (indexedPath != null) {
        return indexedPath;
    }
    SqmFrom<?, ?> path = pathRegistry.findFromByPath(navigablePath.getParent());
    if (path == null) {
        final PluralPersistentAttribute<?, ?, E> referencedPathSource = getReferencedPathSource();
        final SqmFrom<?, Object> parent = pathRegistry.resolveFrom(getLhs());
        final SqmQualifiedJoin<Object, ?> join;
        final SqmExpression<?> index;
        if (referencedPathSource instanceof ListPersistentAttribute<?, ?>) {
            // noinspection unchecked
            join = new SqmListJoin<>(parent, (ListPersistentAttribute<Object, ?>) referencedPathSource, alias, SqmJoinType.INNER, false, parent.nodeBuilder());
            index = ((SqmListJoin<?, ?>) join).index();
        } else if (referencedPathSource instanceof MapPersistentAttribute<?, ?, ?>) {
            // noinspection unchecked
            join = new SqmMapJoin<>(parent, (MapPersistentAttribute<Object, ?, ?>) referencedPathSource, alias, SqmJoinType.INNER, false, parent.nodeBuilder());
            index = ((SqmMapJoin<?, ?, ?>) join).key();
        } else {
            throw new SemanticException("Index access is only supported on list or map attributes: " + getNavigablePath());
        }
        join.setJoinPredicate(creationState.getCreationContext().getNodeBuilder().equal(index, selector));
        parent.addSqmJoin(join);
        pathRegistry.register(path = join);
    }
    final SqmIndexedCollectionAccessPath<Object> result = new SqmIndexedCollectionAccessPath<>(navigablePath, path, selector);
    pathRegistry.register(result);
    return result;
}
Also used : NavigablePath(org.hibernate.query.spi.NavigablePath) ListPersistentAttribute(org.hibernate.metamodel.model.domain.ListPersistentAttribute) SqmPathRegistry(org.hibernate.query.hql.spi.SqmPathRegistry) SemanticException(org.hibernate.query.SemanticException)

Example 2 with SqmPathRegistry

use of org.hibernate.query.hql.spi.SqmPathRegistry 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)

Example 3 with SqmPathRegistry

use of org.hibernate.query.hql.spi.SqmPathRegistry in project hibernate-orm by hibernate.

the class QualifiedJoinPathConsumer method resolveBase.

private ConsumerDelegate resolveBase(String identifier, boolean isTerminal) {
    final SqmCreationProcessingState processingState = creationState.getCurrentProcessingState();
    final SqmPathRegistry pathRegistry = processingState.getPathRegistry();
    final SqmFrom<?, Object> pathRootByAlias = pathRegistry.findFromByAlias(identifier, true);
    if (pathRootByAlias != null) {
        if (isTerminal) {
            throw new SemanticException("Cannot join to root '" + identifier + "'");
        }
        return new AttributeJoinDelegate(pathRootByAlias, joinType, fetch, alias, creationState);
    }
    final SqmFrom<?, Object> pathRootByExposedNavigable = pathRegistry.findFromExposing(identifier);
    if (pathRootByExposedNavigable != null) {
        return new AttributeJoinDelegate(createJoin(pathRootByExposedNavigable, identifier, isTerminal), joinType, fetch, alias, creationState);
    }
    return new ExpectingEntityJoinDelegate(identifier, isTerminal, sqmRoot, joinType, alias, fetch, creationState);
}
Also used : SqmCreationProcessingState(org.hibernate.query.hql.spi.SqmCreationProcessingState) SqmPathRegistry(org.hibernate.query.hql.spi.SqmPathRegistry) SemanticException(org.hibernate.query.SemanticException)

Aggregations

SemanticException (org.hibernate.query.SemanticException)3 SqmPathRegistry (org.hibernate.query.hql.spi.SqmPathRegistry)3 SqmCreationProcessingState (org.hibernate.query.hql.spi.SqmCreationProcessingState)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 HqlParser (org.hibernate.grammars.hql.HqlParser)1 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)1 ListPersistentAttribute (org.hibernate.metamodel.model.domain.ListPersistentAttribute)1 DotIdentifierConsumer (org.hibernate.query.hql.spi.DotIdentifierConsumer)1 NavigablePath (org.hibernate.query.spi.NavigablePath)1 StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)1 UnknownEntityException (org.hibernate.query.sqm.UnknownEntityException)1 SqmCorrelation (org.hibernate.query.sqm.tree.domain.SqmCorrelation)1 SqmPolymorphicRootDescriptor (org.hibernate.query.sqm.tree.domain.SqmPolymorphicRootDescriptor)1 SqmRoot (org.hibernate.query.sqm.tree.from.SqmRoot)1