首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JPA不能反序列化Java 8 LocalDateTime

JPA不能反序列化Java 8 LocalDateTime
EN

Stack Overflow用户
提问于 2017-03-03 03:11:28
回答 4查看 8.8K关注 0票数 4

我使用SpringBoot1.5.1,当我的实体类中有一个LocalDateTime字段时,每当我访问API时都会得到异常。

MySQL dt列是时间戳。

JPA不能本机反序列化LocalDateTime吗?

执行GET请求时控制台输出

代码语言:javascript
复制
 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

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-03-03 06:54:03

代码语言:javascript
复制
@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扫描的包中。将此转换器添加到列声明中

代码语言:javascript
复制
@Column
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime dt;

如果您没有使用JPA2.0,这个answer将帮助您为LocalDateTime使用@Temporal注释。

票数 8
EN

Stack Overflow用户

发布于 2018-02-23 20:45:52

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

代码语言:javascript
复制
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-java8</artifactId>
  <version>${hibernate.version}</version>
</dependency>
票数 2
EN

Stack Overflow用户

发布于 2017-03-03 04:37:01

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

代码语言:javascript
复制
@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();
   }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42569951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档