Prevented possible NullPointerException's
[jpa-converters] / src / main / java / de / juplo / jpa / converters / LocalDateConverter.java
1 package de.juplo.jpa.converters;
2
3 import java.sql.Date;
4 import java.time.LocalDate;
5 import javax.persistence.AttributeConverter;
6 import javax.persistence.Converter;
7
8
9 /**
10  * Converts a {@link LocalDate} to a {@link Date}.
11  * @author Kai Moritz
12  */
13 @Converter(autoApply = true)
14 public class LocalDateConverter implements AttributeConverter<LocalDate, Date>
15 {
16   @Override
17   public Date convertToDatabaseColumn(LocalDate ld)
18   {
19     if (ld == null)
20       return null;
21     return Date.valueOf(ld);
22   }
23
24   @Override
25   public LocalDate convertToEntityAttribute(Date date)
26   {
27     if (date == null)
28       return null;
29     return date.toLocalDate();
30   }
31 }