Search in sources :

Example 16 with JdbcType

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

the class MySQLDialect method getCastTypeName.

@Override
public String getCastTypeName(SqlExpressible type, Long length, Integer precision, Integer scale) {
    final JdbcMapping jdbcMapping = type.getJdbcMapping();
    final JdbcType jdbcType = jdbcMapping.getJdbcType();
    final JavaType<?> javaType = jdbcMapping.getJavaTypeDescriptor();
    switch(jdbcType.getDefaultSqlTypeCode()) {
        case Types.INTEGER:
        case Types.BIGINT:
        case Types.SMALLINT:
        case Types.TINYINT:
            // MySQL doesn't let you cast to INTEGER/BIGINT/TINYINT
            return "signed";
        case Types.BIT:
            // special case for casting to Boolean
            return "unsigned";
        case Types.FLOAT:
        case Types.DOUBLE:
        case Types.REAL:
            // the default scale is 0 (no decimal places)
            return String.format("decimal(%d, %d)", precision == null ? javaType.getDefaultSqlPrecision(this, jdbcType) : precision, scale == null ? javaType.getDefaultSqlScale(this, jdbcType) : scale);
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
            // MySQL doesn't let you cast to BLOB/TINYBLOB/LONGBLOB
            if (length == null) {
                return "binary";
            }
            return String.format("binary(%d)", length);
        case Types.VARCHAR:
        case Types.LONGVARCHAR:
            // MySQL doesn't let you cast to TEXT/LONGTEXT
            if (length == null) {
                return "char";
            }
            return String.format("char(%d)", length);
        default:
            return super.getCastTypeName(type, length, precision, scale);
    }
}
Also used : JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) NullJdbcType(org.hibernate.type.descriptor.jdbc.NullJdbcType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) JsonJdbcType(org.hibernate.type.descriptor.jdbc.JsonJdbcType)

Example 17 with JdbcType

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

the class MetadataBuildingProcess method handleTypes.

// todo (7.0) : buildJandexInitializer
// private static JandexInitManager buildJandexInitializer(
// MetadataBuildingOptions options,
// ClassLoaderAccess classLoaderAccess) {
// final boolean autoIndexMembers = ConfigurationHelper.getBoolean(
// org.hibernate.cfg.AvailableSettings.ENABLE_AUTO_INDEX_MEMBER_TYPES,
// options.getServiceRegistry().getService( ConfigurationService.class ).getSettings(),
// false
// );
// 
// return new JandexInitManager( options.getJandexView(), classLoaderAccess, autoIndexMembers );
// }
private static void handleTypes(BootstrapContext bootstrapContext, MetadataBuildingOptions options) {
    final ClassLoaderService classLoaderService = options.getServiceRegistry().getService(ClassLoaderService.class);
    final TypeConfiguration typeConfiguration = bootstrapContext.getTypeConfiguration();
    final TypeContributions typeContributions = new TypeContributions() {

        @Override
        public void contributeType(BasicType type) {
            getBasicTypeRegistry().register(type);
            conditionallyRegisterJtd(type.getJavaTypeDescriptor());
        }

        private void conditionallyRegisterJtd(JavaType jtd) {
            final JavaTypeRegistry jtdRegistry = getTypeConfiguration().getJavaTypeRegistry();
            jtdRegistry.resolveDescriptor(jtd.getJavaTypeClass(), () -> jtd);
        }

        @Override
        public void contributeType(BasicType type, String... keys) {
            getBasicTypeRegistry().register(type, keys);
            conditionallyRegisterJtd(type.getJavaTypeDescriptor());
        }

        @Override
        public void contributeType(UserType type, String[] keys) {
            contributeType(new CustomType<Object>(type, keys, getTypeConfiguration()));
        }

        @Override
        public void contributeJavaType(JavaType<?> descriptor) {
            typeConfiguration.getJavaTypeRegistry().addDescriptor(descriptor);
        }

        @Override
        public void contributeJdbcType(JdbcType descriptor) {
            typeConfiguration.getJdbcTypeRegistry().addDescriptor(descriptor);
        }

        @Override
        public <T> void contributeType(UserType<T> descriptor) {
            typeConfiguration.getBasicTypeRegistry().register(descriptor, descriptor.returnedClass().getName());
        }

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

        final BasicTypeRegistry getBasicTypeRegistry() {
            return getTypeConfiguration().getBasicTypeRegistry();
        }
    };
    // add Dialect contributed types
    final Dialect dialect = options.getServiceRegistry().getService(JdbcServices.class).getDialect();
    dialect.contributeTypes(typeContributions, options.getServiceRegistry());
    // add TypeContributor contributed types.
    for (TypeContributor contributor : classLoaderService.loadJavaServices(TypeContributor.class)) {
        contributor.contribute(typeContributions, options.getServiceRegistry());
    }
    // add fallback type descriptors
    final JdbcTypeRegistry jdbcTypeRegistry = typeConfiguration.getJdbcTypeRegistry();
    addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.UUID, SqlTypes.BINARY);
    addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.JSON, SqlTypes.VARBINARY);
    addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.INET, SqlTypes.VARBINARY);
    addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.INTERVAL_SECOND, SqlTypes.NUMERIC);
    addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.GEOMETRY, SqlTypes.VARBINARY);
    addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.POINT, SqlTypes.VARBINARY);
    // add explicit application registered types
    typeConfiguration.addBasicTypeRegistrationContributions(options.getBasicTypeRegistrations());
    // For NORMALIZE, we replace the standard types that use TIMESTAMP_WITH_TIMEZONE to use TIMESTAMP
    if (options.getDefaultTimeZoneStorage() == TimeZoneStorageStrategy.NORMALIZE) {
        final JavaTypeRegistry javaTypeRegistry = typeConfiguration.getJavaTypeRegistry();
        final JdbcType timestampDescriptor = jdbcTypeRegistry.getDescriptor(Types.TIMESTAMP);
        final BasicTypeRegistry basicTypeRegistry = typeConfiguration.getBasicTypeRegistry();
        final BasicType<?> offsetDateTimeType = new NamedBasicTypeImpl<>(javaTypeRegistry.getDescriptor(OffsetDateTime.class), timestampDescriptor, "OffsetDateTime");
        final BasicType<?> zonedDateTimeType = new NamedBasicTypeImpl<>(javaTypeRegistry.getDescriptor(ZonedDateTime.class), timestampDescriptor, "ZonedDateTime");
        basicTypeRegistry.register(offsetDateTimeType, "org.hibernate.type.OffsetDateTimeType", OffsetDateTime.class.getSimpleName(), OffsetDateTime.class.getName());
        basicTypeRegistry.register(zonedDateTimeType, "org.hibernate.type.ZonedDateTimeType", ZonedDateTime.class.getSimpleName(), ZonedDateTime.class.getName());
    }
}
Also used : NamedBasicTypeImpl(org.hibernate.type.internal.NamedBasicTypeImpl) BasicType(org.hibernate.type.BasicType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) TypeContributor(org.hibernate.boot.model.TypeContributor) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) JavaType(org.hibernate.type.descriptor.java.JavaType) TypeContributions(org.hibernate.boot.model.TypeContributions) OffsetDateTime(java.time.OffsetDateTime) ZonedDateTime(java.time.ZonedDateTime) Dialect(org.hibernate.dialect.Dialect) BasicTypeRegistry(org.hibernate.type.BasicTypeRegistry) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) UserType(org.hibernate.usertype.UserType) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 18 with JdbcType

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

the class TypeDefinition method createResolution.

private static BasicValue.Resolution<?> createResolution(String name, Class<?> typeImplementorClass, Properties parameters, Map<?, ?> usageSiteProperties, JdbcTypeIndicators indicators, MetadataBuildingContext context) {
    final BootstrapContext bootstrapContext = context.getBootstrapContext();
    final TypeConfiguration typeConfiguration = bootstrapContext.getTypeConfiguration();
    final BeanInstanceProducer instanceProducer = bootstrapContext.getBeanInstanceProducer();
    final boolean isKnownType = Type.class.isAssignableFrom(typeImplementorClass) || UserType.class.isAssignableFrom(typeImplementorClass);
    // support for AttributeConverter would be nice too
    if (isKnownType) {
        final Object typeInstance = instantiateType(bootstrapContext.getServiceRegistry(), name, typeImplementorClass, instanceProducer);
        if (typeInstance instanceof TypeConfigurationAware) {
            ((TypeConfigurationAware) typeInstance).setTypeConfiguration(typeConfiguration);
        }
        final Properties combinedTypeParameters;
        if (CollectionHelper.isNotEmpty(usageSiteProperties)) {
            combinedTypeParameters = new Properties(parameters);
            combinedTypeParameters.putAll(usageSiteProperties);
        } else {
            combinedTypeParameters = parameters;
        }
        injectParameters(typeInstance, combinedTypeParameters);
        if (typeInstance instanceof UserType) {
            final UserType<Object> userType = (UserType<Object>) typeInstance;
            final CustomType<Object> customType = new CustomType<>(userType, typeConfiguration);
            return new UserTypeResolution(customType, null, combinedTypeParameters);
        }
        if (typeInstance instanceof BasicType) {
            final BasicType resolvedBasicType = (BasicType) typeInstance;
            return new BasicValue.Resolution<Object>() {

                @Override
                public JdbcMapping getJdbcMapping() {
                    return resolvedBasicType;
                }

                @Override
                public BasicType getLegacyResolvedBasicType() {
                    return resolvedBasicType;
                }

                @Override
                public Properties getCombinedTypeParameters() {
                    return combinedTypeParameters;
                }

                @Override
                public JavaType<Object> getDomainJavaType() {
                    return resolvedBasicType.getMappedJavaType();
                }

                @Override
                public JavaType<?> getRelationalJavaType() {
                    return resolvedBasicType.getMappedJavaType();
                }

                @Override
                public JdbcType getJdbcType() {
                    return resolvedBasicType.getJdbcType();
                }

                @Override
                public BasicValueConverter getValueConverter() {
                    return null;
                }

                @Override
                public MutabilityPlan<Object> getMutabilityPlan() {
                    // a TypeDefinition does not explicitly provide a MutabilityPlan (yet?)
                    return resolvedBasicType.isMutable() ? getDomainJavaType().getMutabilityPlan() : ImmutableMutabilityPlan.instance();
                }
            };
        }
    }
    if (Serializable.class.isAssignableFrom(typeImplementorClass)) {
        final JavaType<Serializable> jtd = typeConfiguration.getJavaTypeRegistry().resolveDescriptor(typeImplementorClass);
        final JdbcType jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(Types.VARBINARY);
        final BasicType<Serializable> resolved = typeConfiguration.getBasicTypeRegistry().resolve(jtd, jdbcType);
        final SerializableType legacyType = new SerializableType(typeImplementorClass);
        return new BasicValue.Resolution<Object>() {

            @Override
            public JdbcMapping getJdbcMapping() {
                return resolved;
            }

            @Override
            public BasicType getLegacyResolvedBasicType() {
                return legacyType;
            }

            @Override
            public JavaType<Object> getDomainJavaType() {
                return (JavaType) resolved.getMappedJavaType();
            }

            @Override
            public JavaType<?> getRelationalJavaType() {
                return resolved.getMappedJavaType();
            }

            @Override
            public JdbcType getJdbcType() {
                return resolved.getJdbcType();
            }

            @Override
            public BasicValueConverter getValueConverter() {
                return null;
            }

            @Override
            public MutabilityPlan<Object> getMutabilityPlan() {
                // a TypeDefinition does not explicitly provide a MutabilityPlan (yet?)
                return resolved.isMutable() ? getDomainJavaType().getMutabilityPlan() : ImmutableMutabilityPlan.instance();
            }
        };
    }
    throw new IllegalArgumentException("Named type [" + typeImplementorClass + "] did not implement BasicType nor UserType");
}
Also used : BeanInstanceProducer(org.hibernate.resource.beans.spi.BeanInstanceProducer) CustomType(org.hibernate.type.CustomType) TypeConfigurationAware(org.hibernate.type.spi.TypeConfigurationAware) Serializable(java.io.Serializable) BasicType(org.hibernate.type.BasicType) SerializableType(org.hibernate.type.SerializableType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) BootstrapContext(org.hibernate.boot.spi.BootstrapContext) UserTypeResolution(org.hibernate.boot.model.process.internal.UserTypeResolution) Properties(java.util.Properties) BasicType(org.hibernate.type.BasicType) CustomType(org.hibernate.type.CustomType) JavaType(org.hibernate.type.descriptor.java.JavaType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) SerializableType(org.hibernate.type.SerializableType) UserType(org.hibernate.usertype.UserType) Type(org.hibernate.type.Type) JavaType(org.hibernate.type.descriptor.java.JavaType) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) UserType(org.hibernate.usertype.UserType) UserTypeResolution(org.hibernate.boot.model.process.internal.UserTypeResolution)

Example 19 with JdbcType

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

the class InferredBasicValueResolver method ordinalEnumValueResolution.

private static <E extends Enum<E>> InferredBasicValueResolution<E, Integer> ordinalEnumValueResolution(EnumJavaType<E> enumJavaType, BasicJavaType<?> explicitJavaType, JdbcType explicitJdbcType, TypeConfiguration typeConfiguration) {
    final JavaType<Integer> relationalJtd;
    if (explicitJavaType != null) {
        if (!Integer.class.isAssignableFrom(explicitJavaType.getJavaTypeClass())) {
            throw new MappingException("Explicit JavaType [" + explicitJavaType + "] applied to enumerated value with EnumType#ORDINAL" + " should handle `java.lang.Integer` as its relational type descriptor");
        }
        // noinspection unchecked
        relationalJtd = (BasicJavaType<Integer>) explicitJavaType;
    } else {
        relationalJtd = typeConfiguration.getJavaTypeRegistry().getDescriptor(Integer.class);
    }
    final JdbcType jdbcType = explicitJdbcType != null ? explicitJdbcType : typeConfiguration.getJdbcTypeRegistry().getDescriptor(SqlTypes.TINYINT);
    final OrdinalEnumValueConverter<E> valueConverter = new OrdinalEnumValueConverter<>(enumJavaType, jdbcType, relationalJtd);
    return new InferredBasicValueResolution<>(typeConfiguration.getBasicTypeRegistry().resolve(relationalJtd, jdbcType), enumJavaType, relationalJtd, jdbcType, valueConverter, new CustomType<>(new org.hibernate.type.EnumType<>(enumJavaType.getJavaTypeClass(), valueConverter, typeConfiguration), typeConfiguration), ImmutableMutabilityPlan.instance());
}
Also used : OrdinalEnumValueConverter(org.hibernate.metamodel.model.convert.internal.OrdinalEnumValueConverter) EnumType(jakarta.persistence.EnumType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType) MappingException(org.hibernate.MappingException)

Example 20 with JdbcType

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

the class InferredBasicValueResolver method from.

/**
 * Create an inference-based resolution
 */
public static <T> BasicValue.Resolution<T> from(Function<TypeConfiguration, BasicJavaType> explicitJavaTypeAccess, Function<TypeConfiguration, JdbcType> explicitSqlTypeAccess, Type resolvedJavaType, Supplier<JavaType<T>> reflectedJtdResolver, JdbcTypeIndicators stdIndicators, Table table, Selectable selectable, String ownerName, String propertyName, TypeConfiguration typeConfiguration) {
    final BasicJavaType<T> explicitJavaType = explicitJavaTypeAccess != null ? explicitJavaTypeAccess.apply(typeConfiguration) : null;
    final JdbcType explicitJdbcType = explicitSqlTypeAccess != null ? explicitSqlTypeAccess.apply(typeConfiguration) : null;
    return InferredBasicValueResolver.from(explicitJavaType, explicitJdbcType, resolvedJavaType, reflectedJtdResolver, stdIndicators, table, selectable, ownerName, propertyName, typeConfiguration);
}
Also used : JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType)

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