Search in sources :

Example 1 with TimeZoneStorageStrategy

use of org.hibernate.TimeZoneStorageStrategy 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 2 with TimeZoneStorageStrategy

use of org.hibernate.TimeZoneStorageStrategy 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 3 with TimeZoneStorageStrategy

use of org.hibernate.TimeZoneStorageStrategy in project hibernate-orm by hibernate.

the class MetadataBuilderImpl method resolveTimeZoneStorageStrategy.

private static TimeZoneStorageStrategy resolveTimeZoneStorageStrategy(StandardServiceRegistry serviceRegistry, ConfigurationService configService) {
    final TimeZoneStorageType configuredTimeZoneStorageType = configService.getSetting(AvailableSettings.TIMEZONE_DEFAULT_STORAGE, TimeZoneStorageType.class, null);
    final TimeZoneStorageStrategy resolvedTimezoneStorage;
    // For now, we default to NORMALIZE as that is the Hibernate 5.x behavior
    if (configuredTimeZoneStorageType == null) {
        resolvedTimezoneStorage = TimeZoneStorageStrategy.NORMALIZE;
    } else {
        final TimeZoneSupport timeZoneSupport = serviceRegistry.getService(JdbcServices.class).getDialect().getTimeZoneSupport();
        switch(configuredTimeZoneStorageType) {
            case NATIVE:
                if (timeZoneSupport != TimeZoneSupport.NATIVE) {
                    throw new HibernateException("The configured time zone storage type NATIVE is not supported with the configured dialect");
                }
                resolvedTimezoneStorage = TimeZoneStorageStrategy.NATIVE;
                break;
            case NORMALIZE:
                resolvedTimezoneStorage = TimeZoneStorageStrategy.NORMALIZE;
                break;
            default:
                throw new HibernateException("Unsupported time zone storage type: " + configuredTimeZoneStorageType);
        }
    }
    return resolvedTimezoneStorage;
}
Also used : TimeZoneStorageStrategy(org.hibernate.TimeZoneStorageStrategy) HibernateException(org.hibernate.HibernateException) TimeZoneStorageType(org.hibernate.annotations.TimeZoneStorageType) TimeZoneSupport(org.hibernate.dialect.TimeZoneSupport)

Aggregations

TimeZoneStorageStrategy (org.hibernate.TimeZoneStorageStrategy)3 JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)2 JdbcTypeIndicators (org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators)2 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)2 TemporalType (jakarta.persistence.TemporalType)1 HibernateException (org.hibernate.HibernateException)1 TimeZoneStorageType (org.hibernate.annotations.TimeZoneStorageType)1 TimeZoneSupport (org.hibernate.dialect.TimeZoneSupport)1 BasicType (org.hibernate.type.BasicType)1 AttributeConverterJdbcTypeAdapter (org.hibernate.type.descriptor.converter.AttributeConverterJdbcTypeAdapter)1 AttributeConverterTypeAdapter (org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter)1 BasicJavaType (org.hibernate.type.descriptor.java.BasicJavaType)1 JavaType (org.hibernate.type.descriptor.java.JavaType)1