Search in sources :

Example 11 with JdbcType

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

the class InferredBasicValueResolver method stringEnumValueResolution.

private static <E extends Enum<E>> InferredBasicValueResolution<E, String> stringEnumValueResolution(EnumJavaType<E> enumJavaType, BasicJavaType<?> explicitJavaType, JdbcType explicitJdbcType, JdbcTypeIndicators stdIndicators, TypeConfiguration typeConfiguration) {
    final JavaType<String> relationalJtd;
    if (explicitJavaType != null) {
        if (!String.class.isAssignableFrom(explicitJavaType.getJavaTypeClass())) {
            throw new MappingException("Explicit JavaType [" + explicitJavaType + "] applied to enumerated value with EnumType#STRING" + " should handle `java.lang.String` as its relational type descriptor");
        }
        // noinspection unchecked
        relationalJtd = (BasicJavaType<String>) explicitJavaType;
    } else {
        final boolean useCharacter = stdIndicators.getColumnLength() == 1;
        relationalJtd = typeConfiguration.getJavaTypeRegistry().getDescriptor(useCharacter ? Character.class : String.class);
    }
    final JdbcType jdbcType = explicitJdbcType != null ? explicitJdbcType : relationalJtd.getRecommendedJdbcType(stdIndicators);
    final NamedEnumValueConverter<E> valueConverter = new NamedEnumValueConverter<>(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 : NamedEnumValueConverter(org.hibernate.metamodel.model.convert.internal.NamedEnumValueConverter) EnumType(jakarta.persistence.EnumType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType) MappingException(org.hibernate.MappingException)

Example 12 with JdbcType

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

the class BasicValueBinder method prepareAnyDiscriminator.

private void prepareAnyDiscriminator(XProperty modelXProperty) {
    final AnyDiscriminator anyDiscriminatorAnn = findAnnotation(modelXProperty, AnyDiscriminator.class);
    implicitJavaTypeAccess = (typeConfiguration) -> {
        if (anyDiscriminatorAnn != null) {
            switch(anyDiscriminatorAnn.value()) {
                case CHAR:
                    {
                        return Character.class;
                    }
                case INTEGER:
                    {
                        return Integer.class;
                    }
                default:
                    {
                        return String.class;
                    }
            }
        }
        return String.class;
    };
    normalJdbcTypeDetails(modelXProperty);
    normalMutabilityDetails(modelXProperty);
    // layer AnyDiscriminator into the JdbcType resolution
    final Function<TypeConfiguration, JdbcType> originalJdbcTypeResolution = explicitJdbcTypeAccess;
    this.explicitJdbcTypeAccess = (typeConfiguration) -> {
        final JdbcType originalResolution = originalJdbcTypeResolution.apply(typeConfiguration);
        if (originalResolution != null) {
            return originalResolution;
        }
        final Class<?> hintedJavaType = (Class<?>) implicitJavaTypeAccess.apply(typeConfiguration);
        final JavaType<Object> hintedDescriptor = typeConfiguration.getJavaTypeRegistry().getDescriptor(hintedJavaType);
        return hintedDescriptor.getRecommendedJdbcType(typeConfiguration.getCurrentBaseSqlTypeIndicators());
    };
}
Also used : AnyDiscriminator(org.hibernate.annotations.AnyDiscriminator) ListIndexJdbcType(org.hibernate.annotations.ListIndexJdbcType) AnyKeyJdbcType(org.hibernate.annotations.AnyKeyJdbcType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) MapKeyJdbcType(org.hibernate.annotations.MapKeyJdbcType) CollectionIdJdbcType(org.hibernate.annotations.CollectionIdJdbcType) AnyKeyJavaClass(org.hibernate.annotations.AnyKeyJavaClass) MapKeyClass(jakarta.persistence.MapKeyClass) XClass(org.hibernate.annotations.common.reflection.XClass) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration)

Example 13 with JdbcType

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

the class SimpleValue method buildAttributeConverterTypeAdapter.

private <T> AttributeConverterTypeAdapter<T> buildAttributeConverterTypeAdapter(JpaAttributeConverter<T, ?> jpaAttributeConverter) {
    JavaType<T> domainJavaType = jpaAttributeConverter.getDomainJavaType();
    JavaType<?> relationalJavaType = jpaAttributeConverter.getRelationalJavaType();
    // build the SqlTypeDescriptor adapter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Going back to the illustration, this should be a SqlTypeDescriptor that handles the Integer <-> String
    // conversions.  This is the more complicated piece.  First we need to determine the JDBC type code
    // corresponding to the AttributeConverter's declared "databaseColumnJavaType" (how we read that value out
    // of ResultSets).  See JdbcTypeJavaClassMappings for details.  Again, given example, this should return
    // VARCHAR/CHAR
    final JdbcType recommendedJdbcType = relationalJavaType.getRecommendedJdbcType(// todo (6.0) : handle the other JdbcRecommendedSqlTypeMappingContext methods
    new JdbcTypeIndicators() {

        @Override
        public TypeConfiguration getTypeConfiguration() {
            return metadata.getTypeConfiguration();
        }

        @Override
        public TimeZoneStorageStrategy getDefaultTimeZoneStorageStrategy() {
            return buildingContext.getBuildingOptions().getDefaultTimeZoneStorage();
        }
    });
    int jdbcTypeCode = recommendedJdbcType.getDefaultSqlTypeCode();
    if (isLob()) {
        if (LobTypeMappings.isMappedToKnownLobCode(jdbcTypeCode)) {
            jdbcTypeCode = LobTypeMappings.getLobCodeTypeMapping(jdbcTypeCode);
        } else {
            if (Serializable.class.isAssignableFrom(domainJavaType.getJavaTypeClass())) {
                jdbcTypeCode = Types.BLOB;
            } else {
                throw new IllegalArgumentException(String.format(Locale.ROOT, "JDBC type-code [%s (%s)] not known to have a corresponding LOB equivalent, and Java type is not Serializable (to use BLOB)", jdbcTypeCode, JdbcTypeNameMapper.getTypeName(jdbcTypeCode)));
            }
        }
    }
    if (isNationalized()) {
        jdbcTypeCode = NationalizedTypeMappings.toNationalizedTypeCode(jdbcTypeCode);
    }
    // todo : cache the AttributeConverterTypeAdapter in case that AttributeConverter is applied multiple times.
    return new AttributeConverterTypeAdapter<>(ConverterDescriptor.TYPE_NAME_PREFIX + jpaAttributeConverter.getConverterJavaType().getJavaType().getTypeName(), String.format("BasicType adapter for AttributeConverter<%s,%s>", domainJavaType.getJavaType().getTypeName(), relationalJavaType.getJavaType().getTypeName()), jpaAttributeConverter, // calls into the binding/extraction process...
    new AttributeConverterJdbcTypeAdapter(jpaAttributeConverter, metadata.getTypeConfiguration().getJdbcTypeRegistry().getDescriptor(jdbcTypeCode), relationalJavaType), relationalJavaType, domainJavaType, null);
}
Also used : JdbcTypeIndicators(org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators) AttributeConverterJdbcTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterJdbcTypeAdapter) TimeZoneStorageStrategy(org.hibernate.TimeZoneStorageStrategy) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration)

Example 14 with JdbcType

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

the class AttributeConverterTest method testBasicOrmXmlConverterApplication.

@Test
@TestForIssue(jiraKey = "HHH-8462")
public void testBasicOrmXmlConverterApplication() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(Tester.class).addURL(ConfigHelper.findAsResource("org/hibernate/test/converter/orm.xml")).getMetadataBuilder().build();
        final JdbcTypeRegistry jdbcTypeRegistry = metadata.getTypeConfiguration().getJdbcTypeRegistry();
        PersistentClass tester = metadata.getEntityBinding(Tester.class.getName());
        Property nameProp = tester.getProperty("name");
        BasicValue nameValue = (BasicValue) nameProp.getValue();
        Type type = nameValue.getType();
        assertNotNull(type);
        assertThat(type, instanceOf(AttributeConverterTypeAdapter.class));
        final AttributeConverterTypeAdapter typeAdapter = (AttributeConverterTypeAdapter) type;
        assertThat(typeAdapter.getDomainJtd().getJavaTypeClass(), equalTo(String.class));
        assertThat(typeAdapter.getRelationalJtd().getJavaTypeClass(), equalTo(Clob.class));
        final JdbcType jdbcType = typeAdapter.getJdbcType();
        assertThat(jdbcType, is(jdbcTypeRegistry.getDescriptor(Types.CLOB)));
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) AttributeConverterTypeAdapter(org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter) BasicValue(org.hibernate.mapping.BasicValue) StringJavaType(org.hibernate.type.descriptor.java.StringJavaType) EnumJavaType(org.hibernate.type.descriptor.java.EnumJavaType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) AbstractStandardBasicType(org.hibernate.type.AbstractStandardBasicType) Type(org.hibernate.type.Type) Clob(java.sql.Clob) Property(org.hibernate.mapping.Property) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 15 with JdbcType

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

the class Dialect method getCastTypeName.

/**
 * Get the name of the database type appropriate for casting operations
 * (via the CAST() SQL function) for the given {@link SqlExpressible}
 * SQL type.
 *
 * @return The database type name
 */
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();
    Size size;
    if (length == null && precision == null) {
        return getUnboundedTypeName(jdbcType, javaType);
    } else {
        // use the given length/precision/scale
        if (precision != null && scale == null) {
            // needed for cast(x as BigInteger(p))
            scale = javaType.getDefaultSqlScale(Dialect.this, jdbcType);
        }
        size = new Size().setLength(length).setPrecision(precision).setScale(scale);
    }
    return getTypeName(jdbcType, size);
}
Also used : JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) Size(org.hibernate.engine.jdbc.Size) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) NVarcharJdbcType(org.hibernate.type.descriptor.jdbc.NVarcharJdbcType) LongNVarcharJdbcType(org.hibernate.type.descriptor.jdbc.LongNVarcharJdbcType) ClobJdbcType(org.hibernate.type.descriptor.jdbc.ClobJdbcType) NClobJdbcType(org.hibernate.type.descriptor.jdbc.NClobJdbcType) NCharJdbcType(org.hibernate.type.descriptor.jdbc.NCharJdbcType)

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