I have a class with field LocalDate
which I’d like to persist.
@Entity public class Employee { private Long id; private LocalDate issueDate; // ... /// // getters and setters }
When I tried to get an object from database by executing
Employee Employee = em.find(Employee.class, 1L)
I get an exception:
org.eclipse.persistence.exceptions.ConversionException /..../ could not be converted to [class java.time.LocalDate
Then I followed that tutorial and wrote converter between LocalDate and String. It works, but using EclipseLink
and MariaDB
I can only persist date in such a way: “yyyy-MM-dd”. That way I cannot store hours, minutes and seconds. EclipseLink gives the following exception: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
. How do I store more precise time?
My converter:
@Converter(autoApply = true) public class LocalDateConverter implements AttributeConverter<LocalDate, String> { private static final DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd").toFormatter(); @Override public String convertToDatabaseColumn(LocalDate locDate) { return (locDate == null ? null : locDate.format(formatter)); } @Override public LocalDate convertToEntityAttribute(String sqlDate) { return (sqlDate == null ? null : LocalDate.parse(sqlDate, formatter)); } }
Answer
i can see you have an error in your code, since LocalDate have no parameters of time so you got that exception.
to fix your issue you should make those changes to your code in DateTimeFormatter
private static final DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd").toFormatter();
make sure there are no time attr in the DateTimeFormatter