Implemented converters from TemporalAmount into Long for various units
[jpa-converters] / src / main / java / de / juplo / jpa / converters / TemporalAmountToSecondsConverter.java
1 package de.juplo.jpa.converters;
2
3 import java.time.Duration;
4 import java.time.temporal.ChronoUnit;
5 import java.time.temporal.TemporalAmount;
6 import javax.persistence.AttributeConverter;
7 import javax.persistence.Converter;
8
9
10 /**
11  * Converts a {@link TemporalAmount} into a {@link Long} representing seconds.
12  * @author Kai Moritz
13  */
14 @Converter
15 public class TemporalAmountToSecondsConverter implements AttributeConverter<TemporalAmount, Long>
16 {
17   @Override
18   public Long convertToDatabaseColumn(TemporalAmount amount)
19   {
20     return amount.get(ChronoUnit.SECONDS);
21   }
22
23   @Override
24   public TemporalAmount convertToEntityAttribute(Long seconds)
25   {
26     return Duration.ofSeconds(seconds);
27   }
28 }