From: Kai Moritz Date: Tue, 22 Dec 2015 11:03:02 +0000 (+0100) Subject: Converters must convert to a data-type, know by the database X-Git-Tag: jpa-converters-1.0.0~16 X-Git-Url: https://juplo.de/gitweb/?p=jpa-converters;a=commitdiff_plain;h=459423649abd980aee3f093606281819bbe4b83e Converters must convert to a data-type, know by the database For the temporal data-types, these are the classes Date, Time and Timestamp from the package java.sql. --- diff --git a/src/main/java/de/juplo/jpa/converters/InstantConverter.java b/src/main/java/de/juplo/jpa/converters/InstantConverter.java index fe6bbd6..28cf149 100644 --- a/src/main/java/de/juplo/jpa/converters/InstantConverter.java +++ b/src/main/java/de/juplo/jpa/converters/InstantConverter.java @@ -1,33 +1,27 @@ package de.juplo.jpa.converters; +import java.sql.Timestamp; import java.time.Instant; -import java.util.Date; import javax.persistence.AttributeConverter; import javax.persistence.Converter; /** - * Converts an {@link Instant} to a {@link Date}, as suggested in - * the official Java 8 Time tutorial. - *

- * The {@link Date} can then be persisted as - * {@link java.persistene.TemporalType.TIMESTAMP} with the help of the - * {@link java.persist.Tmporal}-annotation. - * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html + * Converts an {@link Instant} to a {@link Timestamp}. * @author Kai Moritz */ @Converter(autoApply = true) -public class InstantConverter implements AttributeConverter +public class InstantConverter implements AttributeConverter { @Override - public Date convertToDatabaseColumn(Instant instant) + public Timestamp convertToDatabaseColumn(Instant instant) { - return Date.from(instant); + return Timestamp.from(instant); } @Override - public Instant convertToEntityAttribute(Date date) + public Instant convertToEntityAttribute(Timestamp ts) { - return date.toInstant(); + return ts.toInstant(); } } diff --git a/src/main/java/de/juplo/jpa/converters/LocalDateConverter.java b/src/main/java/de/juplo/jpa/converters/LocalDateConverter.java index d6a5c51..2606602 100644 --- a/src/main/java/de/juplo/jpa/converters/LocalDateConverter.java +++ b/src/main/java/de/juplo/jpa/converters/LocalDateConverter.java @@ -1,25 +1,13 @@ package de.juplo.jpa.converters; -import java.time.Instant; +import java.sql.Date; import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.ZoneOffset; -import java.util.Date; import javax.persistence.AttributeConverter; import javax.persistence.Converter; /** - * Converts a {@link LocalDate} to a {@link Date}, by converting it into - * an {@Instant} for the GMT-timezone at {@link LocalTime.MIDNIGHT} and then - * convering that {@link Instant} into a {@link Date} as suggested in the - * official Java 8 Time tutorial. - *

- * The {@link Date} can then be persisted as - * {@link java.persistene.TemporalType.DATE} with the help of the - * {@link java.persist.Tmporal}-annotation. - * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html + * Converts a {@link LocalDate} to a {@link Date}. * @author Kai Moritz */ @Converter(autoApply = true) @@ -28,12 +16,12 @@ public class LocalDateConverter implements AttributeConverter @Override public Date convertToDatabaseColumn(LocalDate ld) { - return Date.from(LocalDateTime.of(ld, LocalTime.MIDNIGHT).toInstant(ZoneOffset.UTC)); + return Date.valueOf(ld); } @Override public LocalDate convertToEntityAttribute(Date date) { - return LocalDate.from(date.toInstant()); + return date.toLocalDate(); } } diff --git a/src/main/java/de/juplo/jpa/converters/LocalDateTimeConverter.java b/src/main/java/de/juplo/jpa/converters/LocalDateTimeConverter.java index 0e5251d..24bf45b 100644 --- a/src/main/java/de/juplo/jpa/converters/LocalDateTimeConverter.java +++ b/src/main/java/de/juplo/jpa/converters/LocalDateTimeConverter.java @@ -1,36 +1,27 @@ package de.juplo.jpa.converters; -import java.time.Instant; +import java.sql.Timestamp; import java.time.LocalDateTime; -import java.time.ZoneOffset; -import java.util.Date; import javax.persistence.AttributeConverter; import javax.persistence.Converter; /** - * Converts a {@link LocalDateTime} to a {@link Date}, by converting it into - * an {@Instant} for the GMT-timezone and then convering that {@link Instant} - * into a {@link Date} as suggested in the official Java 8 Time tutorial. - *

- * The {@link Date} can then be persisted as - * {@link java.persistene.TemporalType.TIMESTAMP} with the help of the - * {@link java.persist.Tmporal}-annotation. - * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html + * Converts a {@link LocalDateTime} to a {@link Timestamp}. * @author Kai Moritz */ @Converter(autoApply = true) -public class LocalDateTimeConverter implements AttributeConverter +public class LocalDateTimeConverter implements AttributeConverter { @Override - public Date convertToDatabaseColumn(LocalDateTime ldt) + public Timestamp convertToDatabaseColumn(LocalDateTime ldt) { - return Date.from(ldt.toInstant(ZoneOffset.UTC)); + return Timestamp.valueOf(ldt); } @Override - public LocalDateTime convertToEntityAttribute(Date date) + public LocalDateTime convertToEntityAttribute(Timestamp ts) { - return LocalDateTime.from(date.toInstant()); + return ts.toLocalDateTime(); } } diff --git a/src/main/java/de/juplo/jpa/converters/LocalTimeConverter.java b/src/main/java/de/juplo/jpa/converters/LocalTimeConverter.java index 60019f5..dc7ff99 100644 --- a/src/main/java/de/juplo/jpa/converters/LocalTimeConverter.java +++ b/src/main/java/de/juplo/jpa/converters/LocalTimeConverter.java @@ -1,42 +1,27 @@ package de.juplo.jpa.converters; -import java.time.Instant; -import java.time.LocalDate; -import java.time.LocalDateTime; +import java.sql.Time; import java.time.LocalTime; -import java.time.ZoneOffset; -import java.util.Date; import javax.persistence.AttributeConverter; import javax.persistence.Converter; /** - * Converts a {@link LocalTime} to a {@link Date}, by converting it into - * an {@Instant} for the GMT-timezone at the date of {@link Instant.EPOCH} and - * then convering that {@link Instant} into a {@link Date} as suggested in the - * official Java 8 Time tutorial. - *

- * The {@link Date} can then be persisted as - * {@link java.persistene.TemporalType.TIME} with the help of the - * {@link java.persist.Tmporal}-annotation. - * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html + * Converts a {@link LocalTime} to a {@link Time}. * @author Kai Moritz */ @Converter(autoApply = true) -public class LocalTimeConverter implements AttributeConverter +public class LocalTimeConverter implements AttributeConverter { - private final static LocalDate EPOCH = LocalDate.from(Instant.EPOCH); - - @Override - public Date convertToDatabaseColumn(LocalTime lt) + public Time convertToDatabaseColumn(LocalTime lt) { - return Date.from(LocalDateTime.of(EPOCH, lt).toInstant(ZoneOffset.UTC)); + return Time.valueOf(lt); } @Override - public LocalTime convertToEntityAttribute(Date date) + public LocalTime convertToEntityAttribute(Time time) { - return LocalTime.from(date.toInstant()); + return time.toLocalTime(); } } diff --git a/src/main/java/de/juplo/jpa/converters/OffsetDateTimeConverter.java b/src/main/java/de/juplo/jpa/converters/OffsetDateTimeConverter.java index d49da18..8815cf0 100644 --- a/src/main/java/de/juplo/jpa/converters/OffsetDateTimeConverter.java +++ b/src/main/java/de/juplo/jpa/converters/OffsetDateTimeConverter.java @@ -1,35 +1,28 @@ package de.juplo.jpa.converters; +import java.sql.Timestamp; import java.time.OffsetDateTime; -import java.time.ZonedDateTime; -import java.util.GregorianCalendar; +import java.time.ZoneOffset; import javax.persistence.AttributeConverter; import javax.persistence.Converter; /** - * Converts a {@link OffsetDateTime} to a {@link GregorianCalendar}, by first - * converting it into a {@link ZonedDateTime} and then converting it as - * suggested in the official Java 8 Time tutorial. - *

- * The {@link GregorianCalendar} can then be persisted as - * {@link java.persistene.TemporalType.TIMESTAMP} with the help of the - * {@link java.persist.Tmporal}-annotation. - * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html + * Converts a {@link OffsetDateTime} to a {@link Timestamp}. * @author Kai Moritz */ @Converter(autoApply = true) -public class OffsetDateTimeConverter implements AttributeConverter +public class OffsetDateTimeConverter implements AttributeConverter { @Override - public GregorianCalendar convertToDatabaseColumn(OffsetDateTime odt) + public Timestamp convertToDatabaseColumn(OffsetDateTime odt) { - return GregorianCalendar.from(ZonedDateTime.from(odt)); + return Timestamp.from(odt.withOffsetSameInstant(ZoneOffset.UTC).toInstant()); } @Override - public OffsetDateTime convertToEntityAttribute(GregorianCalendar calendar) + public OffsetDateTime convertToEntityAttribute(Timestamp ts) { - return OffsetDateTime.from(calendar.toZonedDateTime()); + return OffsetDateTime.of(ts.toLocalDateTime(), ZoneOffset.UTC); } } diff --git a/src/main/java/de/juplo/jpa/converters/OffsetTimeConverter.java b/src/main/java/de/juplo/jpa/converters/OffsetTimeConverter.java index 4543950..92de0a9 100644 --- a/src/main/java/de/juplo/jpa/converters/OffsetTimeConverter.java +++ b/src/main/java/de/juplo/jpa/converters/OffsetTimeConverter.java @@ -1,43 +1,29 @@ package de.juplo.jpa.converters; -import java.time.Instant; -import java.time.LocalDate; +import java.sql.Time; import java.time.OffsetTime; import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.util.GregorianCalendar; import javax.persistence.AttributeConverter; import javax.persistence.Converter; /** - * Converts an {@link OffsetTime} to a {@link GregorianCalendar}, by converting - * it into a {@ZonedDateTime} at the date of {@link Instant.EPOCH} and - * then convering that {@link ZonedDateTime} into a {@link GregorianCalendar} - * as suggested in the official Java 8 Time tutorial. - *

- * The {@link GregorianCalendar} can then be persisted as - * {@link java.persistene.TemporalType.TIME} with the help of the - * {@link java.persist.Tmporal}-annotation. - * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html + * Converts an {@link OffsetTime} to a {@link Time}. * @author Kai Moritz */ @Converter(autoApply = true) -public class OffsetTimeConverter implements AttributeConverter +public class OffsetTimeConverter implements AttributeConverter { - private final static LocalDate EPOCH = LocalDate.from(Instant.EPOCH); - - @Override - public GregorianCalendar convertToDatabaseColumn(OffsetTime ot) + public Time convertToDatabaseColumn(OffsetTime ot) { ZoneOffset offset = ot.getOffset(); - return GregorianCalendar.from(ZonedDateTime.of(EPOCH, ot.toLocalTime(), offset)); + return Time.valueOf(ot.withOffsetSameInstant(ZoneOffset.UTC).toLocalTime()); } @Override - public OffsetTime convertToEntityAttribute(GregorianCalendar calendar) + public OffsetTime convertToEntityAttribute(Time time) { - return OffsetTime.from(calendar.toInstant()); + return OffsetTime.of(time.toLocalTime(), ZoneOffset.UTC); } } diff --git a/src/main/java/de/juplo/jpa/converters/ZonedDateTimeConverter.java b/src/main/java/de/juplo/jpa/converters/ZonedDateTimeConverter.java index de9b726..63637ee 100644 --- a/src/main/java/de/juplo/jpa/converters/ZonedDateTimeConverter.java +++ b/src/main/java/de/juplo/jpa/converters/ZonedDateTimeConverter.java @@ -1,33 +1,28 @@ package de.juplo.jpa.converters; +import java.sql.Timestamp; +import java.time.ZoneId; import java.time.ZonedDateTime; -import java.util.GregorianCalendar; import javax.persistence.AttributeConverter; import javax.persistence.Converter; /** - * Converts a {@link ZonedDateTime} to a {@link GregorianCalendar}, as suggested - * in the official Java 8 Time tutorial. - *

- * The {@link GregorianCalendar} can then be persisted as - * {@link java.persistene.TemporalType.TIMESTAMP} with the help of the - * {@link java.persist.Tmporal}-annotation. - * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html + * Converts a {@link ZonedDateTime} to a {@link Timestamp}. * @author Kai Moritz */ @Converter(autoApply = true) -public class ZonedDateTimeConverter implements AttributeConverter +public class ZonedDateTimeConverter implements AttributeConverter { @Override - public GregorianCalendar convertToDatabaseColumn(ZonedDateTime zdt) + public Timestamp convertToDatabaseColumn(ZonedDateTime zdt) { - return GregorianCalendar.from(zdt); + return Timestamp.from(zdt.withZoneSameInstant(ZoneId.systemDefault()).toInstant()); } @Override - public ZonedDateTime convertToEntityAttribute(GregorianCalendar calendar) + public ZonedDateTime convertToEntityAttribute(Timestamp ts) { - return calendar.toZonedDateTime(); + return ZonedDateTime.ofInstant(ts.toInstant(), ZoneId.systemDefault()); } }