BAEL-2521: Moving LocalDateConverter to java-jpa

This commit is contained in:
eric-martin
2019-01-19 18:38:13 -06:00
parent ae232342e7
commit c19558f420

View File

@@ -0,0 +1,25 @@
package com.baeldung.util;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.sql.Date;
import java.time.LocalDate;
import java.util.Optional;
@Converter(autoApply = true)
public class LocalDateConverter implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate localDate) {
return Optional.ofNullable(localDate)
.map(Date::valueOf)
.orElse(null);
}
@Override
public LocalDate convertToEntityAttribute(Date date) {
return Optional.ofNullable(date)
.map(Date::toLocalDate)
.orElse(null);
}
}