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