From e50f5b55edaa8aa2e493e696e612f2c9ea31ce03 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 21 Dec 2015 23:43:55 +0100 Subject: [PATCH] Implemented alternative to-String converters for Duration and Period --- .../jpa/converters/DurationConverter.java | 26 +++++++++++++++++++ .../juplo/jpa/converters/PeriodConverter.java | 26 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/main/java/de/juplo/jpa/converters/DurationConverter.java create mode 100644 src/main/java/de/juplo/jpa/converters/PeriodConverter.java diff --git a/src/main/java/de/juplo/jpa/converters/DurationConverter.java b/src/main/java/de/juplo/jpa/converters/DurationConverter.java new file mode 100644 index 0000000..402dcf3 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/DurationConverter.java @@ -0,0 +1,26 @@ +package de.juplo.jpa.converters; + +import java.time.Duration; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link Duration} into its {@link String} representation. + * @author Kai Moritz + */ +@Converter +public class DurationConverter implements AttributeConverter +{ + @Override + public String convertToDatabaseColumn(Duration duration) + { + return duration.toString(); + } + + @Override + public Duration convertToEntityAttribute(String string) + { + return Duration.parse(string); + } +} diff --git a/src/main/java/de/juplo/jpa/converters/PeriodConverter.java b/src/main/java/de/juplo/jpa/converters/PeriodConverter.java new file mode 100644 index 0000000..b241a56 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/PeriodConverter.java @@ -0,0 +1,26 @@ +package de.juplo.jpa.converters; + +import java.time.Period; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link Period} into its {@link String} representation. + * @author Kai Moritz + */ +@Converter +public class PeriodConverter implements AttributeConverter +{ + @Override + public String convertToDatabaseColumn(Period period) + { + return period.toString(); + } + + @Override + public Period convertToEntityAttribute(String string) + { + return Period.parse(string); + } +} -- 2.20.1