首页 > 解决方案 > 从 MySQL 检索到的 TemporalType.TIMESTAMP 错误

问题描述

我有一个扩展审计表的简单表:

Prestataire 表:

@Entity
@Table(name = "prestataire", catalog = "test")
public class Prestataire extends UserDateAudit implements java.io.Serializable {

    private Integer idaiprestataire;
    private String identifiant;
    private String specialite;
    private String societe;

UserDateAudit 表:

MappedSuperclass
@JsonIgnoreProperties(
        value = {"createdBy", "updatedBy"},
        allowGetters = true
)
public abstract class UserDateAudit extends DateAudit {

    @CreatedBy
    private Long createdBy;

    @LastModifiedBy
    private Long updatedBy;

日期审计表:

package com.timeologik.mc.myjarvia.audit;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import java.io.Serializable;
import java.time.Instant;
import java.util.Date;

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(
        value = {"createdAt", "updatedAt"},
        allowGetters = true
)
public abstract class DateAudit implements Serializable {

    @CreatedDate
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdAt;

    @LastModifiedDate
    @Temporal(TemporalType.TIMESTAMP)
    private Date updatedAt;

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }

}

当我在 prestataire 表中创建记录时,日期没问题:

在此处输入图像描述

但是当我检索值时,时间是错误的:

在此处输入图像描述

这是输出结果的代码:

if (prestataire.getCreatedBy() != null && prestataire.getCreatedAt() != null) {
            System.out.println(prestataire.getIdaiprestataire() + " créé le: " + prestataire.getCreatedAt());

            Calendar calDoc = Calendar.getInstance();
            calDoc.setTime(prestataire.getCreatedAt());
            Calendar newCal = Calendar.getInstance();
            newCal.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
            newCal.set(Calendar.YEAR, calDoc.get(Calendar.YEAR));
            newCal.set(Calendar.MONTH, calDoc.get(Calendar.MONTH));
            newCal.set(Calendar.DAY_OF_MONTH, calDoc.get(Calendar.DAY_OF_MONTH));
            newCal.set(Calendar.HOUR_OF_DAY, calDoc.get(Calendar.HOUR_OF_DAY));
            newCal.set(Calendar.MINUTE, calDoc.get(Calendar.MINUTE));
            newCal.set(Calendar.SECOND, calDoc.get(Calendar.SECOND));
            newCal.set(Calendar.MILLISECOND, calDoc.get(Calendar.MILLISECOND));
            // maj date document avec le bon timezone
            System.out.println("test: " + newCal.getTime());

            this.setCredate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(newCal.getTime()));
            this.setCreuser(service.getUserRepo().findById(prestataire.getCreatedBy()).get().getUsername());
        }

和表结构:

在此处输入图像描述

我该如何解决这个问题?

编辑 2020-01-02

这是我的application.properties:

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url= jdbc:mysql://localhost:3306/myjarvia?useSSL=false&serverTimezone=Europe/Paris&useLegacyDatetimeCode=false
spring.datasource.username= ${db.myjarvia.username}
spring.datasource.password= ${db.myjarvia.password}


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update

## Hibernate Logging
#logging.level.org.hibernate.SQL= DEBUG

## Jackson Properties
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS= false
spring.jackson.time-zone= Europe/Paris

标签: javamysqlhibernatedatetimespring-data-jpa

解决方案


推荐阅读