Skip to content

DATAJDBC-637 - Time conversion now preserve nanosecond precision. #254

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.2.0-SNAPSHOT</version>
<version>2.2.0-DATAJDBC-637-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.2.0-SNAPSHOT</version>
<version>2.2.0-DATAJDBC-637-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.2.0-SNAPSHOT</version>
<version>2.2.0-DATAJDBC-637-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.2.0-SNAPSHOT</version>
<version>2.2.0-DATAJDBC-637-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public BasicJdbcConverter(
MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context,
RelationResolver relationResolver) {

super(context);
super(context, new JdbcCustomConversions());

Assert.notNull(relationResolver, "RelationResolver must not be null");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.jdbc.core.convert;

import java.sql.Timestamp;
import java.time.ZonedDateTime;
import java.time.temporal.Temporal;
import java.util.Date;
Expand Down Expand Up @@ -51,7 +52,7 @@ public Class<?> resolvePrimitiveType(Class<?> type) {

javaToDbType.put(Enum.class, String.class);
javaToDbType.put(ZonedDateTime.class, String.class);
javaToDbType.put(Temporal.class, Date.class);
javaToDbType.put(Temporal.class, Timestamp.class);
}

public abstract Class<?> resolvePrimitiveType(Class<?> type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,28 @@
*/
package org.springframework.data.jdbc.core.convert;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.springframework.data.convert.CustomConversions;
import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes;

/**
* Value object to capture custom conversion. {@link JdbcCustomConversions} also act as factory for
* {@link org.springframework.data.mapping.model.SimpleTypeHolder}
*
* @author Mark Paluch
* @see org.springframework.data.convert.CustomConversions
* @see CustomConversions
* @see org.springframework.data.mapping.model.SimpleTypeHolder
* @see JdbcSimpleTypes
*/
public class JdbcCustomConversions extends org.springframework.data.convert.CustomConversions {
public class JdbcCustomConversions extends CustomConversions {

private static final StoreConversions STORE_CONVERSIONS;
private static final List<Object> STORE_CONVERTERS;

static {

STORE_CONVERTERS = Collections.emptyList();
STORE_CONVERSIONS = StoreConversions.of(JdbcSimpleTypes.HOLDER, STORE_CONVERTERS);
}
private static final List<Object> STORE_CONVERTERS = Arrays
.asList(Jsr310TimestampBasedConverters.getConvertersToRegister().toArray());
private static final StoreConversions STORE_CONVERSIONS = StoreConversions.of(JdbcSimpleTypes.HOLDER,
STORE_CONVERTERS);

/**
* Creates an empty {@link JdbcCustomConversions} object.
Expand All @@ -53,7 +51,15 @@ public JdbcCustomConversions() {
* @param converters must not be {@literal null}.
*/
public JdbcCustomConversions(List<?> converters) {
super(STORE_CONVERSIONS, converters);
super(new ConverterConfiguration(STORE_CONVERSIONS, converters, JdbcCustomConversions::isDateTimeApiConversion));
}

private static boolean isDateTimeApiConversion(
org.springframework.core.convert.converter.GenericConverter.ConvertiblePair cp) {

return (cp.getSourceType().getTypeName().equals("java.util.Date")
&& cp.getTargetType().getTypeName().startsWith("java.time.") //
) || (cp.getTargetType().getTypeName().equals("java.util.Date")
&& cp.getSourceType().getTypeName().startsWith("java.time."));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright 2020 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.convert;

import static java.time.Instant.*;
import static java.time.LocalDateTime.*;
import static java.time.ZoneId.*;

import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.lang.NonNull;

/**
* Helper class to register JSR-310 specific {@link Converter} implementations. These converters are based on
* {@link java.sql.Timestamp} instead of {@link Date} and therefore preserve nanosecond precision
*
* @see org.springframework.data.convert.Jsr310Converters
* @author Jens Schauder
* @since 2.2
*/
public abstract class Jsr310TimestampBasedConverters {

private static final List<Class<?>> CLASSES = Arrays.asList(LocalDateTime.class, LocalDate.class, LocalTime.class,
Instant.class, ZoneId.class, Duration.class, Period.class);

/**
* Returns the converters to be registered. Will only return converters in case we're running on Java 8.
*
* @return
*/
public static Collection<Converter<?, ?>> getConvertersToRegister() {

List<Converter<?, ?>> converters = new ArrayList<>();
converters.add(TimestampToLocalDateTimeConverter.INSTANCE);
converters.add(LocalDateTimeToTimestampConverter.INSTANCE);
converters.add(TimestampToLocalDateConverter.INSTANCE);
converters.add(LocalDateToTimestampConverter.INSTANCE);
converters.add(TimestampToLocalTimeConverter.INSTANCE);
converters.add(LocalTimeToTimestampConverter.INSTANCE);
converters.add(TimestampToInstantConverter.INSTANCE);
converters.add(InstantToTimestampConverter.INSTANCE);

return converters;
}

public static boolean supports(Class<?> type) {

return CLASSES.contains(type);
}

@ReadingConverter
public enum TimestampToLocalDateTimeConverter implements Converter<Timestamp, LocalDateTime> {

INSTANCE;

@NonNull
@Override
public LocalDateTime convert(Timestamp source) {
return ofInstant(source.toInstant(), systemDefault());
}
}

@WritingConverter
public enum LocalDateTimeToTimestampConverter implements Converter<LocalDateTime, Timestamp> {

INSTANCE;

@NonNull
@Override
public Timestamp convert(LocalDateTime source) {
return Timestamp.from(source.atZone(systemDefault()).toInstant());
}
}

@ReadingConverter
public enum TimestampToLocalDateConverter implements Converter<Timestamp, LocalDate> {

INSTANCE;

@NonNull
@Override
public LocalDate convert(Timestamp source) {
return source.toLocalDateTime().toLocalDate();
}
}

@WritingConverter
public enum LocalDateToTimestampConverter implements Converter<LocalDate, Timestamp> {

INSTANCE;

@NonNull
@Override
public Timestamp convert(LocalDate source) {
return Timestamp.from(source.atStartOfDay(systemDefault()).toInstant());
}
}

@ReadingConverter
public enum TimestampToLocalTimeConverter implements Converter<Timestamp, LocalTime> {

INSTANCE;

@NonNull
@Override
public LocalTime convert(Timestamp source) {
return source.toLocalDateTime().toLocalTime();
}
}

@WritingConverter
public enum LocalTimeToTimestampConverter implements Converter<LocalTime, Timestamp> {

INSTANCE;

@NonNull
@Override
public Timestamp convert(LocalTime source) {
return Timestamp.from(source.atDate(LocalDate.now()).atZone(systemDefault()).toInstant());
}
}

@ReadingConverter
public enum TimestampToInstantConverter implements Converter<Timestamp, Instant> {

INSTANCE;

@NonNull
@Override
public Instant convert(Timestamp source) {
return source.toInstant();
}
}

@WritingConverter
public enum InstantToTimestampConverter implements Converter<Instant, Timestamp> {

INSTANCE;

@NonNull
@Override
public Timestamp convert(Instant source) {
return Timestamp.from(source);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import lombok.Value;
import lombok.With;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -36,6 +37,7 @@
import java.util.function.Function;
import java.util.stream.IntStream;

import net.bytebuddy.asm.Advice;
import org.assertj.core.api.SoftAssertions;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -820,6 +822,21 @@ public void resavingAnUnversionedEntity() {
template.save(saved);
}

@Test // DATAJDBC-637
public void saveAndLoadDateTimeWithFullPrecision() {

WithLocalDateTime entity = new WithLocalDateTime();
entity.id = 23L;
entity.testTime = LocalDateTime.of(5, 5, 5, 5, 5, 5, 123456789);

template.insert(entity);

WithLocalDateTime loaded = template.findById(23L, WithLocalDateTime.class);

assertThat(loaded.testTime).isEqualTo(entity.testTime);
}


private <T extends Number> void saveAndUpdateAggregateWithVersion(VersionedAggregate aggregate,
Function<Number, T> toConcreteNumber) {
saveAndUpdateAggregateWithVersion(aggregate, toConcreteNumber, 0);
Expand Down Expand Up @@ -1166,6 +1183,14 @@ void setVersion(Number newVersion) {
}
}

@Table
static class WithLocalDateTime{

@Id
Long id;
LocalDateTime testTime;
}

@Configuration
@Import(TestConfiguration.class)
static class Config {
Expand Down
Loading