Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1904] LocalDateBasicUserType incorrectly converting to java.sql.Timestamp (Multiset fetching) #1905

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalDate> {

public static final BasicUserType<LocalDate> 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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,6 +49,7 @@ public class DocumentForCollections implements Serializable {

private Long id;
private String name;
private LocalDate dateCollected;
private PersonForCollections owner;
private Set<PersonForCollections> partners = new HashSet<PersonForCollections>();
private Map<Integer, PersonForCollections> contacts = new HashMap<Integer, PersonForCollections>();
Expand Down Expand Up @@ -119,6 +121,14 @@ public void setPersonList(List<PersonForCollections> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +42,8 @@ public interface SubviewDocumentMultisetFetchView extends SubviewSimpleDocumentM

public String getName();

public LocalDate getDateCollected();

@Mapping(value = "partners", fetch = FetchStrategy.MULTISET)
public Set<SubviewPersonForCollectionsMultisetFetchView> getMultisetPartners();

Expand Down
Loading