Search in sources :

Example 21 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class InferredBasicValueResolver method fromTemporal.

public static <T> InferredBasicValueResolution<T, T> fromTemporal(TemporalJavaType<T> reflectedJtd, BasicJavaType<?> explicitJavaType, JdbcType explicitJdbcType, Type resolvedJavaType, JdbcTypeIndicators stdIndicators, TypeConfiguration typeConfiguration) {
    final TemporalType requestedTemporalPrecision = stdIndicators.getTemporalPrecision();
    if (explicitJavaType != null) {
        if (!(explicitJavaType instanceof TemporalJavaType)) {
            throw new MappingException("Explicit JavaType [" + explicitJavaType + "] defined for temporal value must implement TemporalJavaType");
        }
        @SuppressWarnings("unchecked") final TemporalJavaType<T> explicitTemporalJtd = (TemporalJavaType<T>) explicitJavaType;
        if (requestedTemporalPrecision != null && explicitTemporalJtd.getPrecision() != requestedTemporalPrecision) {
            throw new MappingException("Temporal precision (`jakarta.persistence.TemporalType`) mismatch... requested precision = " + requestedTemporalPrecision + "; explicit JavaType (`" + explicitTemporalJtd + "`) precision = " + explicitTemporalJtd.getPrecision());
        }
        final JdbcType jdbcType = explicitJdbcType != null ? explicitJdbcType : explicitTemporalJtd.getRecommendedJdbcType(stdIndicators);
        final BasicType<T> jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(explicitTemporalJtd, jdbcType);
        return new InferredBasicValueResolution<>(jdbcMapping, explicitTemporalJtd, explicitTemporalJtd, jdbcType, null, jdbcMapping, explicitTemporalJtd.getMutabilityPlan());
    }
    if (explicitJdbcType != null) {
        final TemporalJavaType<T> jtd;
        if (requestedTemporalPrecision != null) {
            jtd = reflectedJtd.resolveTypeForPrecision(requestedTemporalPrecision, typeConfiguration);
        } else {
            jtd = reflectedJtd;
        }
        final BasicType<T> jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(jtd, explicitJdbcType);
        return new InferredBasicValueResolution<>(jdbcMapping, jtd, jtd, explicitJdbcType, null, jdbcMapping, jtd.getMutabilityPlan());
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Case #3 - no explicit JavaType or JdbcType
    // 
    // - for the moment continue to use the legacy resolution to registered
    // BasicType
    final BasicType<T> basicType;
    if (requestedTemporalPrecision != null && requestedTemporalPrecision != reflectedJtd.getPrecision()) {
        basicType = typeConfiguration.getBasicTypeRegistry().resolve(reflectedJtd.resolveTypeForPrecision(requestedTemporalPrecision, typeConfiguration), TemporalJavaType.resolveJdbcTypeCode(requestedTemporalPrecision));
    } else {
        basicType = typeConfiguration.getBasicTypeRegistry().resolve(reflectedJtd, reflectedJtd.getRecommendedJdbcType(stdIndicators));
    }
    return new InferredBasicValueResolution<>(basicType, basicType.getJavaTypeDescriptor(), basicType.getJavaTypeDescriptor(), basicType.getJdbcType(), null, basicType, reflectedJtd.getMutabilityPlan());
}
Also used : TemporalJavaType(org.hibernate.type.descriptor.java.TemporalJavaType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType) TemporalType(jakarta.persistence.TemporalType) MappingException(org.hibernate.MappingException)

Example 22 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class VersionResolution method from.

// todo (6.0) : support explicit JTD?
// todo (6.0) : support explicit STD?
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <E> VersionResolution<E> from(Function<TypeConfiguration, java.lang.reflect.Type> implicitJavaTypeAccess, Function<TypeConfiguration, BasicJavaType> explicitJtdAccess, Function<TypeConfiguration, JdbcType> explicitStdAccess, TimeZoneStorageType timeZoneStorageType, TypeConfiguration typeConfiguration, @SuppressWarnings("unused") MetadataBuildingContext context) {
    // todo (6.0) : add support for Dialect-specific interpretation?
    final java.lang.reflect.Type implicitJavaType = implicitJavaTypeAccess.apply(typeConfiguration);
    final JavaType registered = typeConfiguration.getJavaTypeRegistry().resolveDescriptor(implicitJavaType);
    final BasicJavaType jtd = (BasicJavaType) registered;
    final JdbcType recommendedJdbcType = jtd.getRecommendedJdbcType(new JdbcTypeIndicators() {

        @Override
        public TypeConfiguration getTypeConfiguration() {
            return typeConfiguration;
        }

        @Override
        public TemporalType getTemporalPrecision() {
            // if it is a temporal version, it needs to be a TIMESTAMP
            return TemporalType.TIMESTAMP;
        }

        @Override
        public TimeZoneStorageStrategy getDefaultTimeZoneStorageStrategy() {
            if (timeZoneStorageType != null) {
                switch(timeZoneStorageType) {
                    case NATIVE:
                        return TimeZoneStorageStrategy.NATIVE;
                    case NORMALIZE:
                        return TimeZoneStorageStrategy.NORMALIZE;
                }
            }
            return context.getBuildingOptions().getDefaultTimeZoneStorage();
        }
    });
    final BasicType<?> basicType = typeConfiguration.getBasicTypeRegistry().resolve(jtd, recommendedJdbcType);
    final BasicType legacyType = typeConfiguration.getBasicTypeRegistry().getRegisteredType(jtd.getJavaType());
    assert legacyType.getJdbcType().equals(recommendedJdbcType);
    return new VersionResolution<>(jtd, recommendedJdbcType, basicType, legacyType);
}
Also used : JdbcTypeIndicators(org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators) TimeZoneStorageStrategy(org.hibernate.TimeZoneStorageStrategy) BasicType(org.hibernate.type.BasicType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) JavaType(org.hibernate.type.descriptor.java.JavaType) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) TemporalType(jakarta.persistence.TemporalType)

Example 23 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class AnnotationBinder method resolveFilterParamType.

private static JdbcMapping resolveFilterParamType(Class<?> type, MetadataBuildingContext context) {
    if (UserType.class.isAssignableFrom(type)) {
        // noinspection unchecked
        return resolveUserType((Class<UserType<?>>) type, context);
    }
    if (AttributeConverter.class.isAssignableFrom(type)) {
        // noinspection unchecked
        return resolveAttributeConverter((Class<AttributeConverter<?, ?>>) type, context);
    }
    if (JavaType.class.isAssignableFrom(type)) {
        // noinspection unchecked
        return resolveJavaType((Class<JavaType<?>>) type, context);
    }
    final TypeConfiguration typeConfiguration = context.getBootstrapContext().getTypeConfiguration();
    final JavaType<Object> jtd = typeConfiguration.getJavaTypeRegistry().findDescriptor(type);
    if (jtd != null) {
        final JdbcType jdbcType = jtd.getRecommendedJdbcType(typeConfiguration.getCurrentBaseSqlTypeIndicators());
        return typeConfiguration.getBasicTypeRegistry().resolve(jtd, jdbcType);
    }
    return null;
}
Also used : AttributeConverter(jakarta.persistence.AttributeConverter) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) MapKeyJavaType(org.hibernate.annotations.MapKeyJavaType) JavaType(org.hibernate.type.descriptor.java.JavaType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) MapKeyJdbcType(org.hibernate.annotations.MapKeyJdbcType) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) CompositeUserType(org.hibernate.usertype.CompositeUserType) UserType(org.hibernate.usertype.UserType)

Example 24 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class BasicValue method buildResolution.

protected Resolution<?> buildResolution() {
    Properties typeParameters = getTypeParameters();
    if (typeParameters != null && Boolean.parseBoolean(typeParameters.getProperty(DynamicParameterizedType.IS_DYNAMIC)) && typeParameters.get(DynamicParameterizedType.PARAMETER_TYPE) == null) {
        createParameterImpl();
    }
    if (explicitTypeName != null) {
        return interpretExplicitlyNamedType(explicitTypeName, enumerationStyle, implicitJavaTypeAccess, explicitJavaTypeAccess, explicitJdbcTypeAccess, explicitMutabilityPlanAccess, getAttributeConverterDescriptor(), typeParameters, this::setTypeParameters, this, typeConfiguration, getBuildingContext());
    }
    if (isVersion()) {
        return VersionResolution.from(implicitJavaTypeAccess, explicitJavaTypeAccess, explicitJdbcTypeAccess, timeZoneStorageType, typeConfiguration, getBuildingContext());
    }
    final ConverterDescriptor attributeConverterDescriptor = getAttributeConverterDescriptor();
    if (attributeConverterDescriptor != null) {
        final ManagedBeanRegistry managedBeanRegistry = getBuildingContext().getBootstrapContext().getServiceRegistry().getService(ManagedBeanRegistry.class);
        final JpaAttributeConverterCreationContext converterCreationContext = new JpaAttributeConverterCreationContext() {

            @Override
            public ManagedBeanRegistry getManagedBeanRegistry() {
                return managedBeanRegistry;
            }

            @Override
            public TypeConfiguration getTypeConfiguration() {
                return typeConfiguration;
            }
        };
        return NamedConverterResolution.from(attributeConverterDescriptor, explicitJavaTypeAccess, explicitJdbcTypeAccess, explicitMutabilityPlanAccess, this, converterCreationContext, getBuildingContext());
    }
    JavaType<?> jtd = null;
    // determine JavaType if we can
    final BasicJavaType explicitJtd;
    if (explicitJavaTypeAccess != null) {
        explicitJtd = explicitJavaTypeAccess.apply(typeConfiguration);
        if (explicitJtd != null) {
            jtd = explicitJtd;
        }
    } else {
        explicitJtd = null;
    }
    if (jtd == null) {
        if (implicitJavaTypeAccess != null) {
            final java.lang.reflect.Type implicitJtd = implicitJavaTypeAccess.apply(typeConfiguration);
            if (implicitJtd != null) {
                jtd = typeConfiguration.getJavaTypeRegistry().getDescriptor(implicitJtd);
            }
        }
    }
    if (jtd == null) {
        final JavaType<?> reflectedJtd = determineReflectedJavaType();
        if (reflectedJtd != null) {
            jtd = reflectedJtd;
        }
    }
    final JdbcType jdbcType;
    if (explicitJdbcTypeAccess != null) {
        jdbcType = explicitJdbcTypeAccess.apply(typeConfiguration);
    } else {
        jdbcType = null;
    }
    if (jtd == null) {
        if (jdbcType != null) {
            jtd = jdbcType.getJdbcRecommendedJavaTypeMapping(null, null, typeConfiguration);
        }
    }
    if (jtd == null) {
        throw new MappingException("Unable to determine JavaType to use : " + this);
    }
    final TypeDefinitionRegistry typeDefinitionRegistry = getBuildingContext().getTypeDefinitionRegistry();
    final TypeDefinition autoAppliedTypeDef = typeDefinitionRegistry.resolveAutoApplied((BasicJavaType<?>) jtd);
    if (autoAppliedTypeDef != null && (!jtd.getJavaTypeClass().isEnum() || enumerationStyle == null)) {
        log.debug("BasicValue resolution matched auto-applied type-definition");
        return autoAppliedTypeDef.resolve(typeParameters, null, getBuildingContext(), this);
    }
    return InferredBasicValueResolver.from(explicitJtd, jdbcType, resolvedJavaType, this::determineReflectedJavaType, this, getTable(), getColumn(), ownerName, propertyName, typeConfiguration);
}
Also used : ConverterDescriptor(org.hibernate.boot.model.convert.spi.ConverterDescriptor) ClassBasedConverterDescriptor(org.hibernate.boot.model.convert.internal.ClassBasedConverterDescriptor) TypeDefinitionRegistry(org.hibernate.boot.model.TypeDefinitionRegistry) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) Properties(java.util.Properties) MappingException(org.hibernate.MappingException) TypeDefinition(org.hibernate.boot.model.TypeDefinition) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) JpaAttributeConverterCreationContext(org.hibernate.boot.model.convert.spi.JpaAttributeConverterCreationContext) ManagedBeanRegistry(org.hibernate.resource.beans.spi.ManagedBeanRegistry)

Example 25 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class ArgumentTypesValidator method validate.

/**
 * We do an initial validation phase with just the SQM tree, even though we don't
 * have all typing information available here (in particular, we don't have the
 * final JDBC type codes for things with converters) because this is the phase
 * that is run at startup for named queries, and can be done in an IDE.
 */
@Override
public void validate(List<? extends SqmTypedNode<?>> arguments, String functionName, QueryEngine queryEngine) {
    delegate.validate(arguments, functionName, queryEngine);
    int count = 0;
    for (SqmTypedNode<?> argument : arguments) {
        JdbcTypeIndicators indicators = queryEngine.getTypeConfiguration().getCurrentBaseSqlTypeIndicators();
        SqmExpressible<?> nodeType = argument.getNodeType();
        FunctionParameterType type = count < types.length ? types[count++] : types[types.length - 1];
        if (nodeType != null) {
            JavaType<?> javaType = nodeType.getExpressibleJavaType();
            if (javaType != null) {
                try {
                    JdbcType jdbcType = javaType.getRecommendedJdbcType(indicators);
                    checkType(count, functionName, type, jdbcType.getJdbcTypeCode(), javaType.getJavaTypeClass());
                } catch (JdbcTypeRecommendationException e) {
                // it's a converter or something like that, and we will check it later
                }
            }
            switch(type) {
                case TEMPORAL_UNIT:
                    if (!(argument instanceof SqmExtractUnit) && !(argument instanceof SqmDurationUnit)) {
                        throwError(type, Object.class, functionName, count);
                    }
                    break;
                // something crazy by the parser
                case TRIM_SPEC:
                    if (!(argument instanceof SqmTrimSpecification)) {
                        throwError(type, Object.class, functionName, count);
                    }
                    break;
                case COLLATION:
                    if (!(argument instanceof SqmCollation)) {
                        throwError(type, Object.class, functionName, count);
                    }
                    break;
            }
        } else {
        // TODO: appropriate error?
        }
    }
}
Also used : JdbcTypeRecommendationException(org.hibernate.type.descriptor.java.spi.JdbcTypeRecommendationException) JdbcTypeIndicators(org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators) SqmTrimSpecification(org.hibernate.query.sqm.tree.expression.SqmTrimSpecification) SqmExtractUnit(org.hibernate.query.sqm.tree.expression.SqmExtractUnit) SqmDurationUnit(org.hibernate.query.sqm.tree.expression.SqmDurationUnit) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) SqmCollation(org.hibernate.query.sqm.tree.expression.SqmCollation)

Aggregations

JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)36 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)11 BasicJavaType (org.hibernate.type.descriptor.java.BasicJavaType)7 BasicValue (org.hibernate.mapping.BasicValue)6 ObjectJdbcType (org.hibernate.type.descriptor.jdbc.ObjectJdbcType)6 MappingException (org.hibernate.MappingException)5 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)5 AttributeConverterTypeAdapter (org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter)5 EnumJavaType (org.hibernate.type.descriptor.java.EnumJavaType)5 JdbcTypeRegistry (org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry)5 MapKeyJdbcType (org.hibernate.annotations.MapKeyJdbcType)4 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)4 AbstractStandardBasicType (org.hibernate.type.AbstractStandardBasicType)4 Type (org.hibernate.type.Type)4 JavaType (org.hibernate.type.descriptor.java.JavaType)4 StringJavaType (org.hibernate.type.descriptor.java.StringJavaType)4 Test (org.junit.Test)4 MetadataSources (org.hibernate.boot.MetadataSources)3 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)3 PersistentClass (org.hibernate.mapping.PersistentClass)3