Implemented alternative to-String converters for Duration and Period
authorKai Moritz <kai@juplo.de>
Mon, 21 Dec 2015 22:43:55 +0000 (23:43 +0100)
committerKai Moritz <kai@juplo.de>
Tue, 15 Aug 2017 17:44:40 +0000 (19:44 +0200)
src/main/java/de/juplo/jpa/converters/DurationConverter.java [new file with mode: 0644]
src/main/java/de/juplo/jpa/converters/PeriodConverter.java [new file with mode: 0644]

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 (file)
index 0000000..402dcf3
--- /dev/null
@@ -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<Duration, String>
+{
+  @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 (file)
index 0000000..b241a56
--- /dev/null
@@ -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<Period, String>
+{
+  @Override
+  public String convertToDatabaseColumn(Period period)
+  {
+    return period.toString();
+  }
+
+  @Override
+  public Period convertToEntityAttribute(String string)
+  {
+    return Period.parse(string);
+  }
+}