<!--
Jackson JSON Processor, required by spring-webmvc. See messageConverters
in rest-config.xml
- -->
+
+ Non required dependency. It is already declared in jackson-datatype-jsr310
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.4</version>
</dependency>
+ -->
<!--
Jackson dependency required for serializing and deserializing LocalDateTime,
LocalDate, etc, etc objects.
+++ /dev/null
-package de.spring.persistence.converters;
-
-import java.sql.Timestamp;
-import java.time.LocalDateTime;
-
-import javax.persistence.AttributeConverter;
-import javax.persistence.Converter;
-
-@Converter(autoApply = true)
-public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
-
- @Override
- public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
- return (localDateTime == null ? null : Timestamp.valueOf(localDateTime));
- }
-
- @Override
- public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
- return (sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime());
- }
-
-}
--- /dev/null
+package de.spring.persistence.converters;
+
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+
+import javax.persistence.AttributeConverter;
+import javax.persistence.Converter;
+
+@Converter(autoApply = true)
+public class OffsetDateTimeAttributeConverter implements AttributeConverter<OffsetDateTime, Timestamp> {
+
+ @Override
+ public Timestamp convertToDatabaseColumn(OffsetDateTime offsetDateTime) {
+ Timestamp timestamp = null;
+
+ if (offsetDateTime != null) {
+ timestamp = Timestamp.valueOf(offsetDateTime.toLocalDateTime());
+ }
+
+ return timestamp;
+ }
+
+ @Override
+ public OffsetDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
+ OffsetDateTime offsetDateTime = null;
+
+ if (sqlTimestamp != null) {
+ final LocalDateTime localDateTime = sqlTimestamp.toLocalDateTime();
+ final ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
+ offsetDateTime = zonedDateTime.toOffsetDateTime();
+ }
+
+ return offsetDateTime;
+ }
+
+}
import java.io.Serializable;
import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
import java.util.Set;
import javax.persistence.Column;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
-import de.spring.persistence.converters.LocalDateTimeAttributeConverter;
+import com.fasterxml.jackson.annotation.JsonFormat;
+
+import de.spring.persistence.converters.OffsetDateTimeAttributeConverter;
@Entity
@Table(name="ad", schema="mybatis_example")
private String adMobileImage;
@NotNull
- @Convert(converter=LocalDateTimeAttributeConverter.class)
+ @Convert(converter=OffsetDateTimeAttributeConverter.class)
@Column(name="created_at", nullable=false)
- private LocalDateTime createdAt;
+ @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ssZ")
+ private OffsetDateTime createdAt;
@NotNull
- @Convert(converter=LocalDateTimeAttributeConverter.class)
+ @Convert(converter=OffsetDateTimeAttributeConverter.class)
@Column(name="updated_at", nullable = false)
- private LocalDateTime updatedAt;
+ @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ssZ")
+ private OffsetDateTime updatedAt;
// It will be used by JPA when filling the property fields with data coming from data base.
protected Ad() {
}
// It will be used by my code (for example by Unit Tests)
- public Ad(Long id, Long companyId, Long companyCategId, String adMobileImage, LocalDateTime createdAt,
- LocalDateTime updatedAt) {
+ public Ad(Long id, Long companyId, Long companyCategId, String adMobileImage, OffsetDateTime createdAt,
+ OffsetDateTime updatedAt) {
this.id = id;
this.companyCategId = companyCategId;
this.adMobileImage = adMobileImage;
return adMobileImage;
}
- public LocalDateTime getCreatedAt() {
+ public OffsetDateTime getCreatedAt() {
return createdAt;
}
- public LocalDateTime getUpdatedAt() {
+ public OffsetDateTime getUpdatedAt() {
return updatedAt;
}
}