【问题标题】:JPA Cannot Deserialize Java 8 LocalDateTimeJPA 无法反序列化 Java 8 LocalDateTime
【发布时间】:2017-03-03 03:11:28
【问题描述】:

我使用的是 Spring Boot 1.5.1,当我的 Entity 类中有 LocalDateTime 字段时,每当我点击我的 API 时都会遇到异常。

MySQL dt 列是 TIMESTAMP

JPA 不能本地反序列化 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>

【问题讨论】:

  • 使用 jpa 转换器完成此任务。
  • 你可以尝试在私有 LocalDateTime dt 上使用 @Temporal(TemporalType.TIMESTAMP) 吗?使用@Column >> 注意你将需要这些导入 1- import javax.persistence.Temporal; 2- 导入 javax.persistence.TemporalType;

标签: java spring hibernate jpa


【解决方案1】:
@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 将帮助您将 @Temporal 注释用于 LocalDateTime。

【讨论】:

【解决方案2】:

如果您使用支持 LocalDateTime 的较新的 hibernate-java8,则不需要转换器。

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-java8</artifactId>
  <version>${hibernate.version}</version>
</dependency>

【讨论】:

  • 该示例显示了具有精选依赖项的 POM。所以这不是一个正确的方法。
【解决方案3】:

您可以如下所述编写转换器:

@Converter(autoApply = true)
public class MyLocalDateConverter implements AttributeConverter<java.time.LocalDate, java.sql.Date> {

   @Override
   public java.sql.Date convertToDatabaseColumn(java.time.LocalDate attribute) {
      return attribute == null ? null : java.sql.Date.valueOf(attribute);
   }

   @Override
   public java.time.LocalDate convertToEntityAttribute(java.sql.Date dbData) {
    return dbData == null ? null : dbData.toLocalDate();
   }
}

【讨论】:

    【解决方案4】:

    你可以在 spring 包中找到一些已经烘焙的转换器:

    org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters
    

    JDocs:

    JPA 2.1 转换器将 JSR-310 类型转换为旧日期。要激活这些转换器,请确保您的持久性提供程序通过将此类包含在映射类列表中来检测它们。在 Spring 环境中,您可以简单地将此类的包(即 org.springframework.data.jpa.convert.threeten)注册为要扫描的包,例如LocalContainerEntityManagerFactoryBean。

    【讨论】:

    • 从“Spring Data JPA 1.8”开始支持。
    猜你喜欢
    • 2017-03-12
    • 2023-04-04
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2018-02-12
    相关资源
    最近更新 更多