HHH-15174 Move annotation handling code to common method for element collection and basic values

This commit is contained in:
Christian Beikov 2022-04-06 09:42:15 +02:00
parent 5a4efc1c58
commit 776bc1d0aa
5 changed files with 82 additions and 36 deletions

View File

@ -149,22 +149,37 @@ public class TimeZoneStorageMappingTests {
);
}
// @Test
// @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsFormat.class)
// public void testNormalize(SessionFactoryScope scope) {
// scope.inSession(
// session -> {
// List<Tuple> resultList = session.createQuery(
// "select e.offsetDateTimeNormalized, extract(offset from e.offsetDateTimeNormalized), e.zonedDateTimeNormalized, extract(offset from e.zonedDateTimeNormalized) from TimeZoneStorageEntity e",
// Tuple.class
// ).getResultList();
// assertThat( resultList.get( 0 ).get( 0, OffsetDateTime.class ), Matchers.is( OFFSET_DATE_TIME ) );
// assertThat( resultList.get( 0 ).get( 1, ZoneOffset.class ), Matchers.is( OFFSET_DATE_TIME.getOffset() ) );
// assertThat( resultList.get( 0 ).get( 2, ZonedDateTime.class ), Matchers.is( ZONED_DATE_TIME ) );
// assertThat( resultList.get( 0 ).get( 3, ZoneOffset.class ), Matchers.is( ZONED_DATE_TIME.getOffset() ) );
// }
// );
// }
@Test
public void testNormalize(SessionFactoryScope scope) {
scope.inSession(
session -> {
List<Tuple> resultList = session.createQuery(
"select e.offsetDateTimeNormalized, e.zonedDateTimeNormalized, e.offsetDateTimeNormalizedUtc, e.zonedDateTimeNormalizedUtc from TimeZoneStorageEntity e",
Tuple.class
).getResultList();
assertThat( resultList.get( 0 ).get( 0, OffsetDateTime.class ).toInstant(), Matchers.is( OFFSET_DATE_TIME.toInstant() ) );
assertThat( resultList.get( 0 ).get( 1, ZonedDateTime.class ).toInstant(), Matchers.is( ZONED_DATE_TIME.toInstant() ) );
assertThat( resultList.get( 0 ).get( 2, OffsetDateTime.class ).toInstant(), Matchers.is( OFFSET_DATE_TIME.toInstant() ) );
assertThat( resultList.get( 0 ).get( 3, ZonedDateTime.class ).toInstant(), Matchers.is( ZONED_DATE_TIME.toInstant() ) );
}
);
}
@Test
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsFormat.class)
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsTimezoneTypes.class)
public void testNormalizeOffset(SessionFactoryScope scope) {
scope.inSession(
session -> {
List<Tuple> resultList = session.createQuery(
"select extract(offset from e.offsetDateTimeNormalizedUtc), extract(offset from e.zonedDateTimeNormalizedUtc) from TimeZoneStorageEntity e",
Tuple.class
).getResultList();
assertThat( resultList.get( 0 ).get( 0, ZoneOffset.class ), Matchers.is( ZoneOffset.systemDefault().getRules().getOffset( OFFSET_DATE_TIME.toInstant() ) ) );
assertThat( resultList.get( 0 ).get( 1, ZoneOffset.class ), Matchers.is( ZoneOffset.UTC ) );
}
);
}
@Entity(name = "TimeZoneStorageEntity")
@Table(name = "TimeZoneStorageEntity")
@ -200,6 +215,14 @@ public class TimeZoneStorageMappingTests {
@Column(name = "birthday_zoned_normalized")
private ZonedDateTime zonedDateTimeNormalized;
@TimeZoneStorage(TimeZoneStorageType.NORMALIZE_UTC)
@Column(name = "birthday_offset_normalized_utc")
private OffsetDateTime offsetDateTimeNormalizedUtc;
@TimeZoneStorage(TimeZoneStorageType.NORMALIZE_UTC)
@Column(name = "birthday_zoned_normalized_utc")
private ZonedDateTime zonedDateTimeNormalizedUtc;
public TimeZoneStorageEntity() {
}
@ -211,6 +234,8 @@ public class TimeZoneStorageMappingTests {
this.zonedDateTimeAuto = zonedDateTime;
this.offsetDateTimeNormalized = offsetDateTime;
this.zonedDateTimeNormalized = zonedDateTime;
this.offsetDateTimeNormalizedUtc = offsetDateTime;
this.zonedDateTimeNormalizedUtc = zonedDateTime;
}
}
}

View File

@ -674,24 +674,6 @@ public class BasicValueBinder implements JdbcTypeIndicators {
enumType = null;
}
final TimeZoneStorage timeZoneStorageAnn = attributeXProperty.getAnnotation( TimeZoneStorage.class );
if ( timeZoneStorageAnn != null ) {
timeZoneStorageType = timeZoneStorageAnn.value();
final TimeZoneColumn timeZoneColumnAnn = attributeXProperty.getAnnotation( TimeZoneColumn.class );
if ( timeZoneColumnAnn != null ) {
if ( timeZoneStorageType != TimeZoneStorageType.AUTO && timeZoneStorageType != TimeZoneStorageType.COLUMN ) {
throw new IllegalStateException(
"@TimeZoneColumn can not be used in conjunction with @TimeZoneStorage( " + timeZoneStorageType +
" ) with attribute " + attributeXProperty.getDeclaringClass().getName() +
'.' + attributeXProperty.getName()
);
}
}
}
else {
timeZoneStorageType = null;
}
normalSupplementalDetails( attributeXProperty);
// layer in support for JPA's approach for specifying a specific Java type for the collection elements...
@ -958,6 +940,24 @@ public class BasicValueBinder implements JdbcTypeIndicators {
if ( temporalAnn != null ) {
temporalPrecision = temporalAnn.value();
}
final TimeZoneStorage timeZoneStorageAnn = attributeXProperty.getAnnotation( TimeZoneStorage.class );
if ( timeZoneStorageAnn != null ) {
timeZoneStorageType = timeZoneStorageAnn.value();
final TimeZoneColumn timeZoneColumnAnn = attributeXProperty.getAnnotation( TimeZoneColumn.class );
if ( timeZoneColumnAnn != null ) {
if ( timeZoneStorageType != TimeZoneStorageType.AUTO && timeZoneStorageType != TimeZoneStorageType.COLUMN ) {
throw new IllegalStateException(
"@TimeZoneColumn can not be used in conjunction with @TimeZoneStorage( " + timeZoneStorageType +
" ) with attribute " + attributeXProperty.getDeclaringClass().getName() +
'.' + attributeXProperty.getName()
);
}
}
}
else {
timeZoneStorageType = null;
}
}
private static Class<? extends UserType<?>> normalizeUserType(Class<? extends UserType<?>> userType) {

View File

@ -11,6 +11,7 @@ import java.sql.Types;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
@ -110,6 +111,10 @@ public class OffsetDateTimeJavaType extends AbstractTemporalJavaType<OffsetDateT
return (X) offsetDateTime.toZonedDateTime();
}
if ( Instant.class.isAssignableFrom( type ) ) {
return (X) offsetDateTime.toInstant();
}
if ( Calendar.class.isAssignableFrom( type ) ) {
return (X) GregorianCalendar.from( offsetDateTime.toZonedDateTime() );
}
@ -169,6 +174,11 @@ public class OffsetDateTimeJavaType extends AbstractTemporalJavaType<OffsetDateT
return OffsetDateTime.of( zonedDateTime.toLocalDateTime(), zonedDateTime.getOffset() );
}
if (value instanceof Instant) {
Instant instant = (Instant) value;
return instant.atOffset( ZoneOffset.UTC );
}
if (value instanceof Timestamp) {
final Timestamp ts = (Timestamp) value;
/*

View File

@ -11,6 +11,7 @@ import java.sql.Types;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
@ -110,6 +111,10 @@ public class ZonedDateTimeJavaType extends AbstractTemporalJavaType<ZonedDateTim
return (X) OffsetDateTime.of( zonedDateTime.toLocalDateTime(), zonedDateTime.getOffset() );
}
if ( Instant.class.isAssignableFrom( type ) ) {
return (X) zonedDateTime.toInstant();
}
if ( Calendar.class.isAssignableFrom( type ) ) {
return (X) GregorianCalendar.from( zonedDateTime );
}
@ -169,6 +174,11 @@ public class ZonedDateTimeJavaType extends AbstractTemporalJavaType<ZonedDateTim
return offsetDateTime.toZonedDateTime();
}
if (value instanceof Instant) {
Instant instant = (Instant) value;
return instant.atZone( ZoneOffset.UTC );
}
if (value instanceof Timestamp) {
final Timestamp ts = (Timestamp) value;
/*

View File

@ -14,6 +14,7 @@ import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.TimeZone;
@ -84,7 +85,7 @@ public class InstantAsTimestampWithTimeZoneJdbcType implements JdbcType {
WrapperOptions wrapperOptions) throws SQLException {
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, wrapperOptions );
// supposed to be supported in JDBC 4.2
st.setObject( index, dateTime, Types.TIMESTAMP_WITH_TIMEZONE );
st.setObject( index, dateTime.withOffsetSameInstant( ZoneOffset.UTC ), Types.TIMESTAMP_WITH_TIMEZONE );
}
@Override
@ -96,7 +97,7 @@ public class InstantAsTimestampWithTimeZoneJdbcType implements JdbcType {
throws SQLException {
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, wrapperOptions );
// supposed to be supported in JDBC 4.2
st.setObject( name, dateTime, Types.TIMESTAMP_WITH_TIMEZONE );
st.setObject( name, dateTime.withOffsetSameInstant( ZoneOffset.UTC ), Types.TIMESTAMP_WITH_TIMEZONE );
}
};
}