java - 从 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
解决方案
推荐阅读
- ruby - 基于 Rails 和 mongoid 中的项目管理用户角色
- json - 如何在 Oracle 18c 中创建手动 JSON?
- ios - Swift 4.2 编译器无法导入使用 Swift 4.0.3 编译的模块
- ios - “致命错误:在使用 JSON 的 Swift 上展开可选值时意外发现 nil”
- angular - 在 stackblitz 中向 Angular 应用程序添加测试用例时出错
- yaml - GitLab CI/CD 通用配置
- node.js - 具有多个字段的 $lookup
- postgresql - Docker Compose 在 Crunchy Postgres 和 Hasura Graphql 引擎上 - 数据库 URL 设置
- selenium - Jenkins 插件在测试构建期间显示文件
- perl - 如何通过 Perl 拆分长文件并打印到不同的文本文件?