java - JPA 无法反序列化 Java 8 LocalDateTime

标签 java spring hibernate jpa

我正在使用 Spring Boot 1.5.1,当我的实体类中有 LocalDateTime 字段时,每当我访问 API 时都会出现异常。

MySQL dt 列是 TIMESTAMP

JPA 是否无法 native 反序列化 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

预订.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 扫描的包中。将此转换器添加到列声明中

@Column
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime dt;

如果您没有使用 JPA 2.0,则此 answer将帮助您使用 LocalDateTime 的 @Temporal 注释。

关于java - JPA 无法反序列化 Java 8 LocalDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42569951/

相关文章:

java - 如何通过 Hibernate 搜索使用通配符和投影创建查询?

Java 编译错误缺少 return 语句。需要澄清

java - 使用带有 Spring security UsernameAndPasswordAuthFilter 的 MultiReadHttpServletRequest 时无法登录

java - 禁用特定方法的 Hibernate 验证

java - java中夏令时的时区问题

3rd 方库中的 java applet AccessControlException

java - 如何配置jackson属性命名策略?

java - Spring-Hibernate DAO 命名约定?

java - 保存实体后,Hibernate 为使用@Formula 注释的实体属性返回 null

java - Hibernate 查询更新