DATAJPA-1174 - Reinstantiated null handling in Jsr310JpaConverters.

Jsr310Converters in Spring Data Commons has been aligned with Spring's general interface contract for parameters (expecting them not to be called with null values) which breaks the JPA AttributeConverter implementations as they have to deal with null values.

We now added the handling previously done in Jsr310Converters to Jsr310JpaConverters.
This commit is contained in:
Oliver Gierke
2017-09-04 14:46:51 +02:00
parent c924493ac0
commit f8f6daeffb
2 changed files with 67 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2017 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.
@@ -52,12 +52,12 @@ public class Jsr310JpaConverters {
@Override
public Date convertToDatabaseColumn(LocalDate date) {
return LocalDateToDateConverter.INSTANCE.convert(date);
return date == null ? null : LocalDateToDateConverter.INSTANCE.convert(date);
}
@Override
public LocalDate convertToEntityAttribute(Date date) {
return DateToLocalDateConverter.INSTANCE.convert(date);
return date == null ? null : DateToLocalDateConverter.INSTANCE.convert(date);
}
}
@@ -66,12 +66,12 @@ public class Jsr310JpaConverters {
@Override
public Date convertToDatabaseColumn(LocalTime time) {
return LocalTimeToDateConverter.INSTANCE.convert(time);
return time == null ? null : LocalTimeToDateConverter.INSTANCE.convert(time);
}
@Override
public LocalTime convertToEntityAttribute(Date date) {
return DateToLocalTimeConverter.INSTANCE.convert(date);
return date == null ? null : DateToLocalTimeConverter.INSTANCE.convert(date);
}
}
@@ -80,12 +80,12 @@ public class Jsr310JpaConverters {
@Override
public Date convertToDatabaseColumn(LocalDateTime date) {
return LocalDateTimeToDateConverter.INSTANCE.convert(date);
return date == null ? null : LocalDateTimeToDateConverter.INSTANCE.convert(date);
}
@Override
public LocalDateTime convertToEntityAttribute(Date date) {
return DateToLocalDateTimeConverter.INSTANCE.convert(date);
return date == null ? null : DateToLocalDateTimeConverter.INSTANCE.convert(date);
}
}
@@ -94,12 +94,12 @@ public class Jsr310JpaConverters {
@Override
public Date convertToDatabaseColumn(Instant instant) {
return InstantToDateConverter.INSTANCE.convert(instant);
return instant == null ? null : InstantToDateConverter.INSTANCE.convert(instant);
}
@Override
public Instant convertToEntityAttribute(Date date) {
return DateToInstantConverter.INSTANCE.convert(date);
return date == null ? null : DateToInstantConverter.INSTANCE.convert(date);
}
}
@@ -107,12 +107,12 @@ public class Jsr310JpaConverters {
public static class ZoneIdConverter implements AttributeConverter<ZoneId, String> {
public String convertToDatabaseColumn(ZoneId zoneId) {
return ZoneIdToStringConverter.INSTANCE.convert(zoneId);
return zoneId == null ? null : ZoneIdToStringConverter.INSTANCE.convert(zoneId);
}
@Override
public ZoneId convertToEntityAttribute(String zoneId) {
return StringToZoneIdConverter.INSTANCE.convert(zoneId);
return zoneId == null ? null : StringToZoneIdConverter.INSTANCE.convert(zoneId);
}
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2017 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
*
* http://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.jpa.convert.threeten;
import static org.assertj.core.api.Assertions.*;
import java.util.Arrays;
import javax.persistence.AttributeConverter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
/**
* Unit tests for {@link Jsr310JpaConverters}.
*
* @author Oliver Gierke
*/
@RunWith(Parameterized.class)
public class Jsr310JpaConvertersUnitTests {
@Parameters
public static Iterable<? extends Object> data() {
return Arrays.asList(new Jsr310JpaConverters.InstantConverter(), //
new Jsr310JpaConverters.LocalDateConverter(), //
new Jsr310JpaConverters.LocalDateTimeConverter(), //
new Jsr310JpaConverters.LocalTimeConverter(), //
new Jsr310JpaConverters.ZoneIdConverter());
}
public @Parameter AttributeConverter<?, ?> converter;
@Test // DATAJPA-
public void convertersHandleNullValuesCorrectly() {
assertThat(converter.convertToDatabaseColumn(null)).isNull();
assertThat(converter.convertToEntityAttribute(null)).isNull();
}
}