Search in sources :

Example 1 with SqlTreeCreationException

use of org.hibernate.sql.ast.SqlTreeCreationException in project hibernate-orm by hibernate.

the class SqmUtil method createValueBindings.

private static void createValueBindings(JdbcParameterBindings jdbcParameterBindings, QueryParameterImplementor<?> domainParam, QueryParameterBinding<?> domainParamBinding, Bindable parameterType, List<JdbcParameter> jdbcParams, Object bindValue, Function<NavigablePath, TableGroup> tableGroupLocator, SharedSessionContractImplementor session) {
    if (parameterType == null) {
        throw new SqlTreeCreationException("Unable to interpret mapping-model type for Query parameter : " + domainParam);
    }
    if (parameterType instanceof EntityIdentifierMapping) {
        final EntityIdentifierMapping identifierMapping = (EntityIdentifierMapping) parameterType;
        final EntityMappingType entityMapping = identifierMapping.findContainingEntityMapping();
        if (entityMapping.getRepresentationStrategy().getInstantiator().isInstance(bindValue, session.getFactory())) {
            bindValue = identifierMapping.getIdentifier(bindValue);
        }
    } else if (parameterType instanceof EntityMappingType) {
        final EntityIdentifierMapping identifierMapping = ((EntityMappingType) parameterType).getIdentifierMapping();
        final EntityMappingType entityMapping = identifierMapping.findContainingEntityMapping();
        parameterType = identifierMapping;
        if (entityMapping.getRepresentationStrategy().getInstantiator().isInstance(bindValue, session.getFactory())) {
            bindValue = identifierMapping.getIdentifier(bindValue);
        }
    } else if (parameterType instanceof EntityAssociationMapping) {
        EntityAssociationMapping association = (EntityAssociationMapping) parameterType;
        bindValue = association.getForeignKeyDescriptor().getAssociationKeyFromSide(bindValue, association.getSideNature().inverse(), session);
        parameterType = association.getForeignKeyDescriptor();
    } else if (parameterType instanceof PluralAttributeMapping) {
        // for now, let's blow up and see where this happens and fix the specifics...
        throw new NotYetImplementedFor6Exception("Binding parameters whose inferred type comes from plural attribute not yet implemented");
    }
    int offset = jdbcParameterBindings.registerParametersForEachJdbcValue(bindValue, Clause.IRRELEVANT, parameterType, jdbcParams, session);
    assert offset == jdbcParams.size();
}
Also used : EntityAssociationMapping(org.hibernate.metamodel.mapping.EntityAssociationMapping) SqlTreeCreationException(org.hibernate.sql.ast.SqlTreeCreationException) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Example 2 with SqlTreeCreationException

use of org.hibernate.sql.ast.SqlTreeCreationException in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method prepareReusablePath.

private TableGroup prepareReusablePath(FromClauseIndex fromClauseIndex, JpaPath<?> sqmPath, Consumer<TableGroup> implicitJoinChecker) {
    final JpaPath<?> parentPath;
    if (sqmPath instanceof SqmTreatedPath<?, ?>) {
        parentPath = ((SqmTreatedPath<?, ?>) sqmPath).getWrappedPath();
    } else {
        parentPath = sqmPath.getParentPath();
    }
    if (parentPath == null) {
        return null;
    }
    final TableGroup tableGroup = fromClauseIndex.findTableGroup(parentPath.getNavigablePath());
    if (tableGroup == null) {
        final TableGroup parentTableGroup = prepareReusablePath(fromClauseIndex, parentPath, implicitJoinChecker);
        if (parentTableGroup == null) {
            final TableGroup parent = fromClauseIndex.findTableGroupOnParents(parentPath.getNavigablePath());
            if (parent != null) {
                throw new SqlTreeCreationException("Found un-correlated path usage in sub query - " + parentPath);
            }
            throw new SqlTreeCreationException("Could not locate TableGroup - " + parentPath.getNavigablePath());
        }
        if (parentPath instanceof SqmTreatedPath<?, ?>) {
            fromClauseIndex.register((SqmPath<?>) parentPath, parentTableGroup);
            return parentTableGroup;
        }
        final TableGroup newTableGroup = createTableGroup(parentTableGroup, (SqmPath<?>) parentPath);
        if (newTableGroup != null) {
            implicitJoinChecker.accept(newTableGroup);
            if (sqmPath instanceof SqmFrom<?, ?>) {
                registerTreatUsage((SqmFrom<?, ?>) sqmPath, newTableGroup);
            }
        }
        return newTableGroup;
    } else if (sqmPath instanceof SqmTreatedPath<?, ?>) {
        fromClauseIndex.register((SqmPath<?>) sqmPath, tableGroup);
        if (sqmPath instanceof SqmFrom<?, ?>) {
            registerTreatUsage((SqmFrom<?, ?>) sqmPath, tableGroup);
        }
    } else if (parentPath instanceof SqmFrom<?, ?>) {
        registerTreatUsage((SqmFrom<?, ?>) parentPath, tableGroup);
    }
    return tableGroup;
}
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) SqmFrom(org.hibernate.query.sqm.tree.from.SqmFrom) SqlTreeCreationException(org.hibernate.sql.ast.SqlTreeCreationException) SqmTreatedPath(org.hibernate.query.sqm.tree.domain.SqmTreatedPath) SelfInterpretingSqmPath(org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) DiscriminatorSqmPath(org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath)

Aggregations

SqlTreeCreationException (org.hibernate.sql.ast.SqlTreeCreationException)2 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)1 EntityAssociationMapping (org.hibernate.metamodel.mapping.EntityAssociationMapping)1 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)1 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)1 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)1 DiscriminatorSqmPath (org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath)1 SelfInterpretingSqmPath (org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath)1 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)1 SqmTreatedPath (org.hibernate.query.sqm.tree.domain.SqmTreatedPath)1 SqmFrom (org.hibernate.query.sqm.tree.from.SqmFrom)1 CorrelatedPluralTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup)1 CorrelatedTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedTableGroup)1 LazyTableGroup (org.hibernate.sql.ast.tree.from.LazyTableGroup)1 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)1 QueryPartTableGroup (org.hibernate.sql.ast.tree.from.QueryPartTableGroup)1 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)1 VirtualTableGroup (org.hibernate.sql.ast.tree.from.VirtualTableGroup)1