Converters must convert to a data-type, know by the database
[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     return Timestamp.valueOf(ldt);
20   }
21
22   @Override
23   public LocalDateTime convertToEntityAttribute(Timestamp ts)
24   {
25     return ts.toLocalDateTime();
26   }
27 }