Converters must convert to a data-type, know by the database
[jpa-converters] / src / main / java / de / juplo / jpa / converters / InstantConverter.java
1 package de.juplo.jpa.converters;
2
3 import java.sql.Timestamp;
4 import java.time.Instant;
5 import javax.persistence.AttributeConverter;
6 import javax.persistence.Converter;
7
8
9 /**
10  * Converts an {@link Instant} to a {@link Timestamp}.
11  * @author Kai Moritz
12  */
13 @Converter(autoApply = true)
14 public class InstantConverter implements AttributeConverter<Instant, Timestamp>
15 {
16   @Override
17   public Timestamp convertToDatabaseColumn(Instant instant)
18   {
19     return Timestamp.from(instant);
20   }
21
22   @Override
23   public Instant convertToEntityAttribute(Timestamp ts)
24   {
25     return ts.toInstant();
26   }
27 }