Search in sources :

Example 6 with SqmSubQuery

use of org.hibernate.query.sqm.tree.select.SqmSubQuery in project hibernate-orm by hibernate.

the class SqmPathRegistryImpl method findFromByAlias.

@Override
public <X extends SqmFrom<?, ?>> X findFromByAlias(String alias, boolean searchParent) {
    final String localAlias = jpaCompliance.isJpaQueryComplianceEnabled() ? alias.toLowerCase(Locale.getDefault()) : alias;
    final SqmFrom<?, ?> registered = sqmFromByAlias.get(localAlias);
    if (registered != null) {
        // noinspection unchecked
        return (X) registered;
    }
    SqmCreationProcessingState parentProcessingState = associatedProcessingState.getParentProcessingState();
    if (searchParent && parentProcessingState != null) {
        X parentRegistered;
        do {
            parentRegistered = parentProcessingState.getPathRegistry().findFromByAlias(alias, false);
            parentProcessingState = parentProcessingState.getParentProcessingState();
        } while (parentProcessingState != null && parentRegistered == null);
        if (parentRegistered != null) {
            // If a parent query contains the alias, we need to create a correlation on the subquery
            final SqmSubQuery<?> selectQuery = (SqmSubQuery<?>) associatedProcessingState.getProcessingQuery();
            SqmFrom<?, ?> correlated;
            if (parentRegistered instanceof Root<?>) {
                correlated = selectQuery.correlate((Root<?>) parentRegistered);
            } else if (parentRegistered instanceof Join<?, ?>) {
                correlated = selectQuery.correlate((Join<?, ?>) parentRegistered);
            } else if (parentRegistered instanceof SqmCrossJoin<?>) {
                correlated = selectQuery.correlate((SqmCrossJoin<?>) parentRegistered);
            } else if (parentRegistered instanceof SqmEntityJoin<?>) {
                correlated = selectQuery.correlate((SqmEntityJoin<?>) parentRegistered);
            } else {
                throw new UnsupportedOperationException("Can't correlate from node: " + parentRegistered);
            }
            register(correlated);
            // noinspection unchecked
            return (X) correlated;
        }
    }
    return null;
}
Also used : SqmSubQuery(org.hibernate.query.sqm.tree.select.SqmSubQuery) SqmCrossJoin(org.hibernate.query.sqm.tree.from.SqmCrossJoin) Root(jakarta.persistence.criteria.Root) SqmCreationProcessingState(org.hibernate.query.hql.spi.SqmCreationProcessingState)

Example 7 with SqmSubQuery

use of org.hibernate.query.sqm.tree.select.SqmSubQuery in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method determineValueMapping.

private MappingModelExpressible<?> determineValueMapping(SqmExpression<?> sqmExpression, FromClauseIndex fromClauseIndex) {
    if (sqmExpression instanceof SqmParameter) {
        return determineValueMapping((SqmParameter<?>) sqmExpression);
    } else if (sqmExpression instanceof SqmPath) {
        log.debugf("Determining mapping-model type for SqmPath : %s ", sqmExpression);
        final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
        return SqmMappingModelHelper.resolveMappingModelExpressible(sqmExpression, domainModel, fromClauseIndex::findTableGroup);
    } else // The model type of an enum literal is always inferred
    if (sqmExpression instanceof SqmEnumLiteral<?>) {
        final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
        if (mappingModelExpressible != null) {
            return mappingModelExpressible;
        }
    } else if (sqmExpression instanceof SqmSubQuery<?>) {
        final SqmSubQuery<?> subQuery = (SqmSubQuery<?>) sqmExpression;
        final SqmSelectClause selectClause = subQuery.getQuerySpec().getSelectClause();
        if (selectClause.getSelections().size() == 1) {
            final SqmSelection<?> subQuerySelection = selectClause.getSelections().get(0);
            final SqmSelectableNode<?> selectableNode = subQuerySelection.getSelectableNode();
            if (selectableNode instanceof SqmExpression<?>) {
                return determineValueMapping((SqmExpression<?>) selectableNode, fromClauseIndex);
            }
            final SqmExpressible<?> selectionNodeType = subQuerySelection.getNodeType();
            if (selectionNodeType != null) {
                final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
                final MappingModelExpressible<?> expressible = domainModel.resolveMappingExpressible(selectionNodeType, this::findTableGroupByPath);
                if (expressible != null) {
                    return expressible;
                }
                try {
                    final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
                    if (mappingModelExpressible != null) {
                        return mappingModelExpressible;
                    }
                } catch (Exception ignore) {
                    return null;
                }
            }
        }
    }
    log.debugf("Determining mapping-model type for generalized SqmExpression : %s", sqmExpression);
    final SqmExpressible<?> nodeType = sqmExpression.getNodeType();
    if (nodeType == null) {
        // We can't determine the type of the expression
        return null;
    }
    final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final MappingModelExpressible<?> valueMapping = domainModel.resolveMappingExpressible(nodeType, fromClauseIndex::getTableGroup);
    if (valueMapping == null) {
        final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
        if (mappingModelExpressible != null) {
            return mappingModelExpressible;
        }
    }
    if (valueMapping == null) {
        // For literals it is totally possible that we can't figure out a mapping type
        if (sqmExpression instanceof SqmLiteral<?>) {
            return null;
        }
        throw new ConversionException("Could not determine ValueMapping for SqmExpression: " + sqmExpression);
    }
    return valueMapping;
}
Also used : SqmSubQuery(org.hibernate.query.sqm.tree.select.SqmSubQuery) SqmSelectableNode(org.hibernate.query.sqm.tree.select.SqmSelectableNode) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) SqmSelectClause(org.hibernate.query.sqm.tree.select.SqmSelectClause) SelfInterpretingSqmPath(org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) DiscriminatorSqmPath(org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath) SqmEnumLiteral(org.hibernate.query.sqm.tree.expression.SqmEnumLiteral) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException) SemanticException(org.hibernate.query.SemanticException) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) SqlTreeCreationException(org.hibernate.sql.ast.SqlTreeCreationException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) InterpretationException(org.hibernate.query.sqm.InterpretationException) SQLException(java.sql.SQLException) SqmExpressible(org.hibernate.query.sqm.SqmExpressible) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) SqmParameter(org.hibernate.query.sqm.tree.expression.SqmParameter) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral)

Aggregations

SqmSubQuery (org.hibernate.query.sqm.tree.select.SqmSubQuery)7 HqlParser (org.hibernate.grammars.hql.HqlParser)3 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)3 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 SemanticException (org.hibernate.query.SemanticException)2 SqmCreationProcessingState (org.hibernate.query.hql.spi.SqmCreationProcessingState)2 StrictJpaComplianceViolation (org.hibernate.query.sqm.StrictJpaComplianceViolation)2 SqmLiteral (org.hibernate.query.sqm.tree.expression.SqmLiteral)2 SqmFromClause (org.hibernate.query.sqm.tree.from.SqmFromClause)2 SqmPredicate (org.hibernate.query.sqm.tree.predicate.SqmPredicate)2 SqmSelectClause (org.hibernate.query.sqm.tree.select.SqmSelectClause)2 SqmSelection (org.hibernate.query.sqm.tree.select.SqmSelection)2 Root (jakarta.persistence.criteria.Root)1 BigInteger (java.math.BigInteger)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HibernateException (org.hibernate.HibernateException)1 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)1 QueryException (org.hibernate.QueryException)1 MultipleBagFetchException (org.hibernate.loader.MultipleBagFetchException)1