我想在数据库中存储没有时间的日期。所以,我选择使用LocalDate
类型。
如本文所述:How to persist LocalDate and LocalDateTime with JPA 2.1,我使用JPA转换器将LocalDate
转换为Date
。
然而,当我想持久化实体(使用POST和PUT请求)时,我遇到了一些麻烦。
- 错误**
2019-02-23 11:26:30.254 WARN 2720 --- [-auto-1-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Expected array or string.; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string.
at [Source: (PushbackInputStream); line: 1, column: 104] (through reference chain: ...entity.MyObject["startdate"])]
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.springframework.http.ResponseEntity]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.http.ResponseEntity` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 2]
- 验证码**
- 转换 *
package ...entity;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.time.LocalDate;
import java.sql.Date;
@Converter(autoApply = true)
public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate locDate) {
return (locDate == null ? null : Date.valueOf(locDate));
}
@Override
public LocalDate convertToEntityAttribute(Date sqlDate) {
return (sqlDate == null ? null : sqlDate.toLocalDate());
}
}
- 实体 *
package ...entity;
import org.hibernate.annotations.ColumnDefault;
import javax.persistence.*;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;
@Entity
public class MyObject {
@Id
private String id;
private LocalDate startdate;
private LocalDate enddate;
public MyObject() {}
public MyObject(LocalDate enddate) {
this.startdate = LocalDate.now();
this.enddate = enddate;
}
...
}
- "主"*
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
MyObject myobject = new MyObject(LocalDate.parse("2019-03-01", formatter));
谢谢你的帮助
- 编辑1:打印MyObject**
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(this.toJsonString(myObject), headers);
System.out.println(entity.toString());
// <{"id":"ba6649e4-6e65-4f54-8f1a-f8fc7143b05a","startdate":{"year":2019,"month":"FEBRUARY","dayOfMonth":23,"dayOfWeek":"SATURDAY","era":"CE","dayOfYear":54,"leapYear":false,"monthValue":2,"chronology":{"id":"ISO","calendarType":"iso8601"}},"enddate":{"year":2019,"month":"MARCH","dayOfMonth":1,"dayOfWeek":"FRIDAY","era":"CE","dayOfYear":60,"leapYear":false,"monthValue":3,"chronology":{"id":"ISO","calendarType":"iso8601"}}},[Content-Type:"application/json"]>
5条答案
按热度按时间ogq8wdun1#
在JPA 2.2中,您不再需要使用转换器,它增加了对以下java.time类型Map的支持:
ds97pgxw2#
JPA 2.2支持
LocalDate
,因此不需要转换器。Hibernate从5.3版本开始也支持它。
看看这篇文章:What’s new in JPA 2.2 – Java 8 Date and Time Types了解更多详情。
webghufk3#
JPA 2.2增加了对MapJava 8日期/时间API的支持,如
LocalDate
、LocalTime
、LocalDateTime
、OffsetDateTime
或OffsetTime
。假设我们有以下实体:
注意,
subscribedOn
属性是一个LocalDate
Java对象。当持久化
UserAccount
时:Hibernate生成正确的SQL语句:
在获取
UserAccount
实体时,我们可以看到LocalDate
是从数据库中正确获取的:w9apscun4#
Hibernate 5支持java 8,所以你可以把它添加到你的pom.xml中:
这为您提供了
LocalDate
和LocalDateTime
的Map。b09cbbtk5#
我想你可以写自己的转换器,请检查一个答案:Spring Data JPA - Conversion failed when converting date and/or time from character string