Search in sources :

Example 1 with CompositeSqmPathSource

use of org.hibernate.metamodel.model.domain.internal.CompositeSqmPathSource in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method determineValueMapping.

protected MappingModelExpressible<?> determineValueMapping(SqmParameter<?> sqmParameter) {
    log.debugf("Determining mapping-model type for SqmParameter : %s", sqmParameter);
    final QueryParameterImplementor<?> queryParameter = domainParameterXref.getQueryParameter(sqmParameter);
    final QueryParameterBinding<?> binding = domainParameterBindings.getBinding(queryParameter);
    BindableType<?> paramType = binding.getBindType();
    if (paramType == null) {
        paramType = queryParameter.getHibernateType();
        if (paramType == null) {
            paramType = sqmParameter.getAnticipatedType();
        }
    }
    if (paramType == null) {
        final MappingModelExpressible<?> inferredValueMapping = getInferredValueMapping();
        if (inferredValueMapping != null) {
            return inferredValueMapping;
        }
        // Default to the Object type
        return basicType(Object.class);
    } else if (paramType instanceof MappingModelExpressible<?>) {
        final MappingModelExpressible<?> paramModelType = (MappingModelExpressible<?>) paramType;
        final MappingModelExpressible<?> inferredValueMapping = getInferredValueMapping();
        // Prefer the model part type instead of the bind type if possible as the model part type contains size information
        if (inferredValueMapping instanceof ModelPart) {
            final JdbcMapping paramJdbcMapping = paramModelType.getJdbcMappings().get(0);
            final JdbcMapping inferredJdbcMapping = inferredValueMapping.getJdbcMappings().get(0);
            // If the bind type has a different JDBC type, we prefer that
            if (paramJdbcMapping.getJdbcType() == inferredJdbcMapping.getJdbcType()) {
                return inferredValueMapping;
            }
        }
        return paramModelType;
    } else if (sqmParameter.getAnticipatedType() == null) {
        // this should indicate the condition that the user query did not define an
        // explicit type in regard to this parameter.  Here we should prefer the
        // inferrable type and fallback to resolving the binding type
        final MappingModelExpressible<?> inferredValueMapping = getInferredValueMapping();
        if (inferredValueMapping != null) {
            return inferredValueMapping;
        }
    } else if (paramType instanceof EntityDomainType) {
        // In JPA Criteria, it is possible to define a parameter of an entity type,
        // but that should infer the mapping type from context,
        // otherwise this would default to binding the PK which might be wrong
        final MappingModelExpressible<?> inferredValueMapping = getInferredValueMapping();
        if (inferredValueMapping != null) {
            return inferredValueMapping;
        }
    }
    final SqmExpressible<?> paramSqmType = paramType.resolveExpressible(creationContext.getSessionFactory());
    if (paramSqmType instanceof SqmPath) {
        final SqmPath<?> sqmPath = (SqmPath<?>) paramSqmType;
        final NavigablePath navigablePath = sqmPath.getNavigablePath();
        if (navigablePath.getParent() != null) {
            final TableGroup tableGroup = getFromClauseAccess().getTableGroup(navigablePath.getParent());
            return tableGroup.getModelPart().findSubPart(navigablePath.getLocalName(), null);
        }
        return getFromClauseAccess().getTableGroup(navigablePath).getModelPart();
    }
    if (paramSqmType instanceof BasicValuedMapping) {
        return (BasicValuedMapping) paramSqmType;
    }
    if (paramSqmType instanceof CompositeSqmPathSource) {
        // Try to infer the value mapping since the other side apparently is a path source
        final MappingModelExpressible<?> inferredValueMapping = getInferredValueMapping();
        if (inferredValueMapping != null) {
            return inferredValueMapping;
        }
        throw new NotYetImplementedFor6Exception("Support for embedded-valued parameters not yet implemented");
    }
    if (paramSqmType instanceof SqmPathSource<?> || paramSqmType instanceof BasicDomainType<?>) {
        // Try to infer the value mapping since the other side apparently is a path source
        final MappingModelExpressible<?> inferredValueMapping = getInferredValueMapping();
        if (inferredValueMapping != null) {
            return inferredValueMapping;
        }
        return getTypeConfiguration().getBasicTypeForJavaType(paramSqmType.getExpressibleJavaType().getJavaTypeClass());
    }
    throw new ConversionException("Could not determine ValueMapping for SqmParameter: " + sqmParameter);
}
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) 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) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) SelfInterpretingSqmPath(org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) DiscriminatorSqmPath(org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath) CompositeSqmPathSource(org.hibernate.metamodel.model.domain.internal.CompositeSqmPathSource) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType)

Aggregations

NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)1 BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)1 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)1 ConvertibleModelPart (org.hibernate.metamodel.mapping.ConvertibleModelPart)1 EmbeddableValuedModelPart (org.hibernate.metamodel.mapping.EmbeddableValuedModelPart)1 EntityValuedModelPart (org.hibernate.metamodel.mapping.EntityValuedModelPart)1 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)1 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)1 ModelPart (org.hibernate.metamodel.mapping.ModelPart)1 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)1 CompositeSqmPathSource (org.hibernate.metamodel.model.domain.internal.CompositeSqmPathSource)1 DiscriminatorSqmPath (org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath)1 NavigablePath (org.hibernate.query.spi.NavigablePath)1 SelfInterpretingSqmPath (org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath)1 SqmPath (org.hibernate.query.sqm.tree.domain.SqmPath)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