Search in sources :

Example 26 with JdbcType

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

the class BasicTypeRegistry method resolveTypeReference.

private BasicType<?> resolveTypeReference(String name) {
    final BasicTypeReference<?> typeReference = typeReferencesByName.get(name);
    if (typeReference == null) {
        return null;
    }
    if (!name.equals(typeReference.getName())) {
        final BasicType<?> basicType = typesByName.get(typeReference.getName());
        if (basicType != null) {
            return basicType;
        }
    }
    final JavaType<Object> javaType = typeConfiguration.getJavaTypeRegistry().getDescriptor(typeReference.getBindableJavaType());
    final JdbcType jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(typeReference.getSqlTypeCode());
    final BasicType<?> type;
    if (typeReference.getConverter() == null) {
        if (typeReference.isForceImmutable()) {
            type = new ImmutableNamedBasicTypeImpl<>(javaType, jdbcType, typeReference.getName());
        } else {
            type = new NamedBasicTypeImpl<>(javaType, jdbcType, typeReference.getName());
        }
    } else {
        if (typeReference.isForceImmutable()) {
            // noinspection unchecked
            type = new ImmutableConvertedBasicTypeImpl<>(javaType, jdbcType, typeReference.getName(), (BasicValueConverter<Object, ?>) typeReference.getConverter());
        } else {
            // noinspection unchecked
            type = new ConvertedBasicTypeImpl<>(javaType, jdbcType, typeReference.getName(), (BasicValueConverter<Object, ?>) typeReference.getConverter());
        }
    }
    primeRegistryEntry(type);
    typesByName.put(typeReference.getName(), type);
    typesByName.put(name, type);
    return type;
}
Also used : JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) BasicValueConverter(org.hibernate.metamodel.model.convert.spi.BasicValueConverter)

Example 27 with JdbcType

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

the class EnumType method setParameterValues.

@Override
public void setParameterValues(Properties parameters) {
    // IMPL NOTE: we handle 2 distinct cases here:
    // 1) we are passed a ParameterType instance in the incoming Properties - generally
    // speaking this indicates the annotation-binding case, and the passed ParameterType
    // represents information about the attribute and annotation
    // 2) we are not passed a ParameterType - generally this indicates a hbm.xml binding case.
    final ParameterType reader = (ParameterType) parameters.get(PARAMETER_TYPE);
    // handles hbm.xml
    if (reader != null) {
        enumClass = (Class<T>) reader.getReturnedClass().asSubclass(Enum.class);
        final Long columnLength = reader.getColumnLengths()[0];
        final boolean isOrdinal;
        final jakarta.persistence.EnumType enumType = getEnumType(reader);
        if (enumType == null) {
            isOrdinal = true;
        } else if (jakarta.persistence.EnumType.ORDINAL.equals(enumType)) {
            isOrdinal = true;
        } else if (jakarta.persistence.EnumType.STRING.equals(enumType)) {
            isOrdinal = false;
        } else {
            throw new AssertionFailure("Unknown EnumType: " + enumType);
        }
        final EnumJavaType enumJavaType = (EnumJavaType) typeConfiguration.getJavaTypeRegistry().getDescriptor(enumClass);
        final LocalJdbcTypeIndicators indicators = new LocalJdbcTypeIndicators(enumType, columnLength, reader);
        final BasicJavaType<?> relationalJavaType = resolveRelationalJavaType(indicators, enumJavaType);
        final JdbcType jdbcType = relationalJavaType.getRecommendedJdbcType(indicators);
        if (isOrdinal) {
            this.enumValueConverter = new OrdinalEnumValueConverter(enumJavaType, jdbcType, relationalJavaType);
        } else {
            this.enumValueConverter = new NamedEnumValueConverter(enumJavaType, jdbcType, relationalJavaType);
        }
        this.jdbcType = jdbcType;
    } else {
        final String enumClassName = (String) parameters.get(ENUM);
        try {
            enumClass = ReflectHelper.classForName(enumClassName, this.getClass()).asSubclass(Enum.class);
        } catch (ClassNotFoundException exception) {
            throw new HibernateException("Enum class not found: " + enumClassName, exception);
        }
        this.enumValueConverter = interpretParameters(parameters);
        this.jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(enumValueConverter.getJdbcTypeCode());
    }
    this.jdbcValueExtractor = (ValueExtractor) jdbcType.getExtractor(enumValueConverter.getRelationalJavaType());
    this.jdbcValueBinder = (ValueBinder) jdbcType.getBinder(enumValueConverter.getRelationalJavaType());
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Using %s-based conversion for Enum %s", isOrdinal() ? "ORDINAL" : "NAMED", enumClass.getName());
    }
}
Also used : NamedEnumValueConverter(org.hibernate.metamodel.model.convert.internal.NamedEnumValueConverter) AssertionFailure(org.hibernate.AssertionFailure) HibernateException(org.hibernate.HibernateException) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) EnumJavaType(org.hibernate.type.descriptor.java.EnumJavaType) OrdinalEnumValueConverter(org.hibernate.metamodel.model.convert.internal.OrdinalEnumValueConverter)

Example 28 with JdbcType

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

the class SerializableToBlobType method getCastType.

@Override
public CastType getCastType() {
    final JdbcType jdbcType = getJdbcType();
    final int jdbcTypeCode = jdbcType.getJdbcTypeCode();
    switch(jdbcTypeCode) {
        case Types.BIT:
        case Types.SMALLINT:
        case Types.TINYINT:
        case Types.INTEGER:
            if (getJavaType() == Boolean.class) {
                return CastType.INTEGER_BOOLEAN;
            }
            break;
        case Types.CHAR:
        case Types.NCHAR:
            if (getJavaType() == Boolean.class) {
                return (Boolean) getJavaTypeDescriptor().wrap('Y', null) ? CastType.YN_BOOLEAN : CastType.TF_BOOLEAN;
            }
            break;
        case Types.TIMESTAMP_WITH_TIMEZONE:
            if (getJavaType() == ZonedDateTime.class) {
                return CastType.ZONE_TIMESTAMP;
            }
            break;
    }
    return jdbcType.getCastType();
}
Also used : JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) BlobJdbcType(org.hibernate.type.descriptor.jdbc.BlobJdbcType)

Example 29 with JdbcType

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

the class EnumResolutionTests method verifyEnumResolution.

@SuppressWarnings("rawtypes")
private void verifyEnumResolution(Property property, int jdbcCode, Class<?> javaType, Consumer<BasicValueConverter> converterChecker, Consumer<BasicType> legacyTypeChecker) {
    final BasicValue.Resolution<?> resolution = ((BasicValue) property.getValue()).resolve();
    final TypeConfiguration typeConfiguration = ((BasicValue) property.getValue()).getTypeConfiguration();
    final JdbcType jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(jdbcCode);
    // verify the interpretations used for reading
    assertThat(resolution.getJdbcType(), is(jdbcType));
    assertThat(resolution.getRelationalJavaType().getJavaTypeClass(), equalTo(javaType));
    assertThat(resolution.getDomainJavaType().getJavaTypeClass(), equalTo(Values.class));
    final JdbcMapping jdbcMapping = resolution.getJdbcMapping();
    assertThat(jdbcMapping.getJdbcType(), equalTo(resolution.getJdbcType()));
    assertThat(jdbcMapping.getJavaTypeDescriptor(), equalTo(resolution.getRelationalJavaType()));
    converterChecker.accept(resolution.getValueConverter());
    // verify the (legacy) interpretations used for writing
    legacyTypeChecker.accept(resolution.getLegacyResolvedBasicType());
}
Also used : JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) BasicValue(org.hibernate.mapping.BasicValue)

Example 30 with JdbcType

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

the class AbstractSelectionQuery method verifyResultType.

protected static <T> void verifyResultType(Class<T> resultClass, SqmExpressible<?> sqmExpressible, SessionFactoryImplementor sessionFactory) {
    assert sqmExpressible != null;
    final JavaType<?> expressibleJavaType = sqmExpressible.getExpressibleJavaType();
    assert expressibleJavaType != null;
    final Class<?> javaTypeClass = expressibleJavaType.getJavaTypeClass();
    if (!resultClass.isAssignableFrom(javaTypeClass)) {
        if (expressibleJavaType instanceof PrimitiveJavaType) {
            if (((PrimitiveJavaType) expressibleJavaType).getPrimitiveClass() == resultClass) {
                return;
            }
            throwQueryTypeMismatchException(resultClass, sqmExpressible);
        }
        // But the expected resultClass could be a subtype of that, so we need to check the JdbcType
        if (javaTypeClass == Date.class) {
            JdbcType jdbcType = null;
            if (sqmExpressible instanceof BasicDomainType<?>) {
                jdbcType = ((BasicDomainType<?>) sqmExpressible).getJdbcType();
            } else if (sqmExpressible instanceof SqmPathSource<?>) {
                final DomainType<?> domainType = ((SqmPathSource<?>) sqmExpressible).getSqmPathType();
                if (domainType instanceof BasicDomainType<?>) {
                    jdbcType = ((BasicDomainType<?>) domainType).getJdbcType();
                }
            }
            if (jdbcType != null) {
                switch(jdbcType.getJdbcTypeCode()) {
                    case Types.DATE:
                        if (resultClass.isAssignableFrom(java.sql.Date.class)) {
                            return;
                        }
                        break;
                    case Types.TIME:
                        if (resultClass.isAssignableFrom(java.sql.Time.class)) {
                            return;
                        }
                        break;
                    case Types.TIMESTAMP:
                        if (resultClass.isAssignableFrom(java.sql.Timestamp.class)) {
                            return;
                        }
                        break;
                }
            }
        }
        throwQueryTypeMismatchException(resultClass, sqmExpressible);
    }
}
Also used : BasicDomainType(org.hibernate.metamodel.model.domain.BasicDomainType) DomainType(org.hibernate.metamodel.model.domain.DomainType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) PrimitiveJavaType(org.hibernate.type.descriptor.java.spi.PrimitiveJavaType) BasicDomainType(org.hibernate.metamodel.model.domain.BasicDomainType)

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