JPA无法反序列化Java 8 LocalDateTime [英] JPA Cannot Deserialize Java 8 LocalDateTime
问题描述
我正在使用Spring Boot 1.5.1,并且当我的Entity类中有LocalDateTime字段时,只要碰到我的API,都会得到一个异常.
I'm using Spring Boot 1.5.1 and getting an exception anytime I hit my API when there is a LocalDateTime field in my Entity class.
MySQL dt列是TIMESTAMP
The MySQL dt column is TIMESTAMP
JPA无法本地反序列化LocalDateTime吗?
Is JPA not able to natively deserialize LocalDateTime?
执行GET请求时控制台输出
2017-03-02 22:00:18.797 ERROR 13736 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize] with root cause
java.io.StreamCorruptedException: invalid stream header: 20323031
Reservation.class
package com.example.springboot.reservation;
import java.time.LocalDateTime;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
// Model class
@Entity
@Table(name="reservation")
public class Reservation {
@Id
private Long id;
@Column
private LocalDateTime dt;
@Column(name="user_id")
private Long userId;
// Hibernate will convert camel case column names to snake case!!!
// Don't use camelcase columns in DB
@Column(name="party_size")
private int partySize;
public Reservation() {}
public Reservation(Long id, Long userId, int partySize) {
this.id = id;
this.userId = userId;
this.partySize = partySize;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public LocalDateTime getDt() {
return dt;
}
public void setDt(LocalDateTime dt) {
this.dt = dt;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public int getPartySize() {
return partySize;
}
public void setPartySize(int partySize) {
this.partySize = partySize;
}
}
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
推荐答案
@Converter
public class LocalDateTimeConverter implements AttributeConverter<java.time.LocalDateTime, java.sql.Timestamp> {
@Override
public java.sql.Timestamp convertToDatabaseColumn(java.time.LocalDateTime entityValue) {
return entityValue == null ? null : java.sql.Timestamp.valueOf(entityValue)
}
@Override
public java.time.LocalDateTime convertToEntityAttribute(java.sql.Timestamp dbValue) {
return dbValue == null ? null : dbValue.toLocalDateTime();
}
}
确保将此转换器类添加到由hibernate扫描的程序包中.将此转换器添加到列声明中
Make sure that this converter class is added to the package scanned by hibernate. Add this converter to the column declaration
@Column
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime dt;
如果您不使用JPA 2.0,则此 answer 可以帮助您将@Temporal
注释用于LocalDateTime.
If you are not using JPA 2.0, This answer would help you use @Temporal
annotation for LocalDateTime.
这篇关于JPA无法反序列化Java 8 LocalDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!