Introduce a special TimeZoneStorageType#NORMALIZE_UTC variant

This commit is contained in:
Christian Beikov 2022-03-25 20:04:34 +01:00
parent f2aa533dfc
commit f048ea8205
9 changed files with 57 additions and 17 deletions

View File

@ -23,8 +23,12 @@ public enum TimeZoneStorageStrategy {
* Stores the time zone in a separate column.
*/
COLUMN,
/**
* Doesn't store the time zone, but instead normalizes to the JDBC timezone.
*/
NORMALIZE,
/**
* Doesn't store the time zone, but instead normalizes to UTC.
*/
NORMALIZE
NORMALIZE_UTC
}

View File

@ -28,9 +28,14 @@ public enum TimeZoneStorageType {
NATIVE,
/**
* Does not store the time zone, and instead normalizes
* timestamps to UTC.
* timestamps to the JDBC timezone.
*/
NORMALIZE,
/**
* Does not store the time zone, and instead normalizes
* timestamps to UTC.
*/
NORMALIZE_UTC,
/**
* Stores the time zone in a separate column; works in
* conjunction with {@link TimeZoneColumn}.

View File

@ -894,6 +894,9 @@ public class MetadataBuilderImpl implements MetadataBuilderImplementor, TypeCont
case NORMALIZE:
resolvedTimezoneStorage = TimeZoneStorageStrategy.NORMALIZE;
break;
case NORMALIZE_UTC:
resolvedTimezoneStorage = TimeZoneStorageStrategy.NORMALIZE_UTC;
break;
case AUTO:
switch ( timeZoneSupport ) {
case NATIVE:

View File

@ -71,6 +71,8 @@ public class VersionResolution<E> implements BasicValue.Resolution<E> {
return TimeZoneStorageStrategy.NATIVE;
case NORMALIZE:
return TimeZoneStorageStrategy.NORMALIZE;
case NORMALIZE_UTC:
return TimeZoneStorageStrategy.NORMALIZE_UTC;
}
}
return context.getBuildingOptions().getDefaultTimeZoneStorage();

View File

@ -400,23 +400,33 @@ public class MetadataBuildingProcess {
);
// add explicit application registered types
typeConfiguration
.addBasicTypeRegistrationContributions( options.getBasicTypeRegistrations() );
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 JdbcType timestampWithTimeZoneOverride;
switch ( options.getDefaultTimeZoneStorage() ) {
case NORMALIZE:
// For NORMALIZE, we replace the standard types that use TIMESTAMP_WITH_TIMEZONE to use TIMESTAMP
timestampWithTimeZoneOverride = jdbcTypeRegistry.getDescriptor( Types.TIMESTAMP );
break;
case NORMALIZE_UTC:
// For NORMALIZE_UTC, we replace the standard types that use TIMESTAMP_WITH_TIMEZONE to use TIMESTAMP_UTC
timestampWithTimeZoneOverride = jdbcTypeRegistry.getDescriptor( SqlTypes.TIMESTAMP_UTC );
break;
default:
timestampWithTimeZoneOverride = null;
break;
}
if ( timestampWithTimeZoneOverride != null ) {
final JavaTypeRegistry javaTypeRegistry = typeConfiguration.getJavaTypeRegistry();
final BasicTypeRegistry basicTypeRegistry = typeConfiguration.getBasicTypeRegistry();
final BasicType<?> offsetDateTimeType = new NamedBasicTypeImpl<>(
javaTypeRegistry.getDescriptor( OffsetDateTime.class ),
timestampDescriptor,
timestampWithTimeZoneOverride,
"OffsetDateTime"
);
final BasicType<?> zonedDateTimeType = new NamedBasicTypeImpl<>(
javaTypeRegistry.getDescriptor( ZonedDateTime.class ),
timestampDescriptor,
timestampWithTimeZoneOverride,
"ZonedDateTime"
);
basicTypeRegistry.register(

View File

@ -185,6 +185,8 @@ public class BasicValueBinder implements JdbcTypeIndicators {
return TimeZoneStorageStrategy.NATIVE;
case NORMALIZE:
return TimeZoneStorageStrategy.NORMALIZE;
case NORMALIZE_UTC:
return TimeZoneStorageStrategy.NORMALIZE_UTC;
}
}
return buildingContext.getBuildingOptions().getDefaultTimeZoneStorage();

View File

@ -694,6 +694,8 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
return TimeZoneStorageStrategy.NATIVE;
case NORMALIZE:
return TimeZoneStorageStrategy.NORMALIZE;
case NORMALIZE_UTC:
return TimeZoneStorageStrategy.NORMALIZE_UTC;
}
}
return getBuildingContext().getBuildingOptions().getDefaultTimeZoneStorage();

View File

@ -22,6 +22,7 @@ import jakarta.persistence.TemporalType;
import org.hibernate.TimeZoneStorageStrategy;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.SqlTypes;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
@ -55,9 +56,14 @@ public class OffsetDateTimeJavaType extends AbstractTemporalJavaType<OffsetDateT
final JdbcTypeRegistry jdbcTypeRegistry = stdIndicators.getTypeConfiguration()
.getJdbcTypeRegistry();
if ( temporalPrecision == null || temporalPrecision == TemporalType.TIMESTAMP ) {
return stdIndicators.getDefaultTimeZoneStorageStrategy() == TimeZoneStorageStrategy.NORMALIZE
? jdbcTypeRegistry.getDescriptor( Types.TIMESTAMP )
: jdbcTypeRegistry.getDescriptor( Types.TIMESTAMP_WITH_TIMEZONE );
switch ( stdIndicators.getDefaultTimeZoneStorageStrategy() ) {
case NORMALIZE:
return jdbcTypeRegistry.getDescriptor( Types.TIMESTAMP );
case NORMALIZE_UTC:
return jdbcTypeRegistry.getDescriptor( SqlTypes.TIMESTAMP_UTC );
default:
return jdbcTypeRegistry.getDescriptor( Types.TIMESTAMP_WITH_TIMEZONE );
}
}
switch ( temporalPrecision ) {

View File

@ -23,6 +23,7 @@ import org.hibernate.TimeZoneStorageStrategy;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.ZonedDateTimeComparator;
import org.hibernate.type.SqlTypes;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
@ -55,9 +56,14 @@ public class ZonedDateTimeJavaType extends AbstractTemporalJavaType<ZonedDateTim
final JdbcTypeRegistry jdbcTypeRegistry = stdIndicators.getTypeConfiguration()
.getJdbcTypeRegistry();
if ( temporalPrecision == null || temporalPrecision == TemporalType.TIMESTAMP ) {
return stdIndicators.getDefaultTimeZoneStorageStrategy() == TimeZoneStorageStrategy.NORMALIZE
? jdbcTypeRegistry.getDescriptor( Types.TIMESTAMP )
: jdbcTypeRegistry.getDescriptor( Types.TIMESTAMP_WITH_TIMEZONE );
switch ( stdIndicators.getDefaultTimeZoneStorageStrategy() ) {
case NORMALIZE:
return jdbcTypeRegistry.getDescriptor( Types.TIMESTAMP );
case NORMALIZE_UTC:
return jdbcTypeRegistry.getDescriptor( SqlTypes.TIMESTAMP_UTC );
default:
return jdbcTypeRegistry.getDescriptor( Types.TIMESTAMP_WITH_TIMEZONE );
}
}
switch ( temporalPrecision ) {