diff --git a/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java b/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java index 791bb6d8b8..9273a7d167 100644 --- a/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java +++ b/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java @@ -18,22 +18,46 @@ import com.blazebit.persistence.view.spi.type.BasicUserType; -import java.sql.Timestamp; import java.time.LocalDate; -import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.DateTimeParseException; /** - * * @author Christian Beikov * @since 1.5.0 */ public class LocalDateBasicUserType extends TimestampishImmutableBasicUserType { public static final BasicUserType INSTANCE = new LocalDateBasicUserType(); + private static final DateTimeFormatter OPTIONAL_TIME_OFFSET_AND_ZONE_FORMATTER = new DateTimeFormatterBuilder() + .parseCaseInsensitive() + .append(DateTimeFormatter.ISO_LOCAL_DATE) + .optionalStart() + .optionalStart() + .appendLiteral(' ') + .optionalEnd() + .optionalStart() + .appendLiteral('T') + .optionalEnd() + .append(DateTimeFormatter.ISO_LOCAL_TIME) + .optionalStart() + .appendOffsetId() + .optionalEnd() + .optionalStart() + .appendLiteral('[') + .appendZoneRegionId() + .appendLiteral(']') + .optionalEnd() + .optionalEnd().toFormatter(); @Override public LocalDate fromString(CharSequence sequence) { - return Timestamp.valueOf(sequence.toString()).toInstant().atZone(ZoneOffset.UTC).toLocalDate(); + String input = sequence.toString(); + try { + return LocalDate.parse(input, OPTIONAL_TIME_OFFSET_AND_ZONE_FORMATTER); + } catch (DateTimeParseException e) { + throw new IllegalArgumentException("Invalid date or date-time format: " + input, e); + } } - } diff --git a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/entity/simple/DocumentForCollections.java b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/entity/simple/DocumentForCollections.java index 516b899648..aad77ad8f8 100644 --- a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/entity/simple/DocumentForCollections.java +++ b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/entity/simple/DocumentForCollections.java @@ -28,6 +28,7 @@ import javax.persistence.OrderColumn; import javax.persistence.Table; import java.io.Serializable; +import java.time.LocalDate; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -48,6 +49,7 @@ public class DocumentForCollections implements Serializable { private Long id; private String name; + private LocalDate dateCollected; private PersonForCollections owner; private Set partners = new HashSet(); private Map contacts = new HashMap(); @@ -119,6 +121,14 @@ public void setPersonList(List personList) { this.personList = personList; } + public LocalDate getDateCollected() { + return dateCollected; + } + + public void setDateCollected(LocalDate dateCollected) { + this.dateCollected = dateCollected; + } + @Override public int hashCode() { final int prime = 31; diff --git a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/MultisetFetchCollectionsTest.java b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/MultisetFetchCollectionsTest.java index 1b036530a2..fe1c3b0b71 100644 --- a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/MultisetFetchCollectionsTest.java +++ b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/MultisetFetchCollectionsTest.java @@ -41,6 +41,7 @@ import org.junit.experimental.categories.Category; import javax.persistence.EntityManager; +import java.time.LocalDate; import java.util.List; import java.util.Map; import java.util.Set; @@ -91,6 +92,8 @@ public void work(EntityManager em) { doc1.getContacts().put(1, pers1); doc1.getContacts().put(2, pers2); + doc1.setDateCollected(LocalDate.now()); + em.persist(pers1); em.persist(pers2); diff --git a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/model/SubviewDocumentMultisetFetchView.java b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/model/SubviewDocumentMultisetFetchView.java index 21292f70db..e2021e6529 100644 --- a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/model/SubviewDocumentMultisetFetchView.java +++ b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/model/SubviewDocumentMultisetFetchView.java @@ -23,6 +23,7 @@ import com.blazebit.persistence.view.UpdatableEntityView; import com.blazebit.persistence.view.testsuite.collections.entity.simple.DocumentForCollections; +import java.time.LocalDate; import java.util.List; import java.util.Map; import java.util.Set; @@ -41,6 +42,8 @@ public interface SubviewDocumentMultisetFetchView extends SubviewSimpleDocumentM public String getName(); + public LocalDate getDateCollected(); + @Mapping(value = "partners", fetch = FetchStrategy.MULTISET) public Set getMultisetPartners();