Skip to content

Fix missing class issue with H2 version 2.0.202 #1115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>2.4.0-1114-update-H2-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>2.4.0-1114-update-H2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>2.4.0-1114-update-H2-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>2.4.0-1114-update-H2-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.core.dialect;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import org.h2.api.TimestampWithTimeZone;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;

/**
* Converter converting from an H2 internal representation of a timestamp with time zone to an OffsetDateTime.
*
* Only required for H2 versions < 2.0
*
* @author Jens Schauder
* @since 2.7
*/
@ReadingConverter
public enum H2TimestampWithTimeZoneToOffsetDateTimeConverter
implements Converter<TimestampWithTimeZone, OffsetDateTime> {

INSTANCE;

@Override
public OffsetDateTime convert(TimestampWithTimeZone source) {

long nanosInSecond = 1_000_000_000;
long nanosInMinute = nanosInSecond * 60;
long nanosInHour = nanosInMinute * 60;

long hours = (source.getNanosSinceMidnight() / nanosInHour);

long nanosInHours = hours * nanosInHour;
long nanosLeft = source.getNanosSinceMidnight() - nanosInHours;
long minutes = nanosLeft / nanosInMinute;

long nanosInMinutes = minutes * nanosInMinute;
nanosLeft -= nanosInMinutes;
long seconds = nanosLeft / nanosInSecond;

long nanosInSeconds = seconds * nanosInSecond;
nanosLeft -= nanosInSeconds;
ZoneOffset offset = ZoneOffset.ofTotalSeconds(source.getTimeZoneOffsetSeconds());

return OffsetDateTime.of(source.getYear(), source.getMonth(), source.getDay(), (int) hours, (int) minutes,
(int) seconds, (int) nanosLeft, offset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@
*/
package org.springframework.data.jdbc.core.dialect;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.h2.api.TimestampWithTimeZone;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.relational.core.dialect.Db2Dialect;
import org.springframework.data.relational.core.dialect.H2Dialect;

Expand All @@ -43,39 +38,26 @@ protected JdbcH2Dialect() {}
@Override
public Collection<Object> getConverters() {

List<Object> converters = new ArrayList<>(super.getConverters());
converters.add(TimestampWithTimeZoneToOffsetDateTimeConverter.INSTANCE);
return converters;
}

@ReadingConverter
enum TimestampWithTimeZoneToOffsetDateTimeConverter implements Converter<TimestampWithTimeZone, OffsetDateTime> {

INSTANCE;
final Collection<Object> originalConverters = super.getConverters();

@Override
public OffsetDateTime convert(TimestampWithTimeZone source) {
if (isH2belowVersion2()) {

long nanosInSecond = 1_000_000_000;
long nanosInMinute = nanosInSecond * 60;
long nanosInHour = nanosInMinute * 60;

long hours = (source.getNanosSinceMidnight() / nanosInHour);
List<Object> converters = new ArrayList<>(originalConverters);
converters.add(H2TimestampWithTimeZoneToOffsetDateTimeConverter.INSTANCE);
return converters;
}

long nanosInHours = hours * nanosInHour;
long nanosLeft = source.getNanosSinceMidnight() - nanosInHours;
long minutes = nanosLeft / nanosInMinute;
return originalConverters;
}

long nanosInMinutes = minutes * nanosInMinute;
nanosLeft -= nanosInMinutes;
long seconds = nanosLeft / nanosInSecond;
static boolean isH2belowVersion2() {

long nanosInSeconds = seconds * nanosInSecond;
nanosLeft -= nanosInSeconds;
ZoneOffset offset = ZoneOffset.ofTotalSeconds(source.getTimeZoneOffsetSeconds());
try {

return OffsetDateTime.of(source.getYear(), source.getMonth(), source.getDay(), (int) hours, (int) minutes,
(int) seconds, (int) nanosLeft, offset);
JdbcH2Dialect.class.getClassLoader().loadClass("org.h2.api.TimestampWithTimeZone");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class JdbcH2DialectTests {
@Test
void TimestampWithTimeZone2OffsetDateTimeConverterConvertsProperly() {

JdbcH2Dialect.TimestampWithTimeZoneToOffsetDateTimeConverter converter = JdbcH2Dialect.TimestampWithTimeZoneToOffsetDateTimeConverter.INSTANCE;
H2TimestampWithTimeZoneToOffsetDateTimeConverter converter = H2TimestampWithTimeZoneToOffsetDateTimeConverter.INSTANCE;
long dateValue = 123456789;
long timeNanos = 987654321;
int timeZoneOffsetSeconds = 4 * 60 * 60;
Expand Down
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>2.4.0-1114-update-H2-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>2.4.0-1114-update-H2-SNAPSHOT</version>
</parent>

<properties>
Expand Down