【问题标题】:How to use EnableJpaAuditing annotation in Spring boot to wrtite @CreatedDate in Indian Standard Time i.e. UTC+05:30?如何在 Spring Boot 中使用 EnableJpaAuditing 注释在印度标准时间(即 UTC+05:30)中写入 @CreatedDate?
【发布时间】:2020-06-27 07:39:43
【问题描述】:

我的模型扩展了这个 AuditModel

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.*;
import java.io.Serializable;
import java.util.Date;

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(
        value = {"createdAt", "updatedAt"},
        allowGetters = true
)
public abstract class AuditModel implements Serializable {
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at", nullable = false, updatable = false)
    @CreatedDate
    private Date createdAt;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated_at", nullable = false)
    @LastModifiedDate
    private Date updatedAt;

     //getters and setters

}

我已将以下行添加到 application.properties

spring.jpa.properties.hibernate.jdbc.time_zone=UTC

但我希望数据库条目中的时间为 UTC+05:30。该怎么做?

谢谢

【问题讨论】:

    标签: spring hibernate jpa spring-data-jpa audit


    【解决方案1】:

    在您的主应用程序类或其他适当的 bean 中,您可以添加一个 PostConstruct 以在整个应用程序中设置时区,这将被审计标签认可(以下是 UTC 作为示例)

    @PostConstruct
    public void init() {
        TimeZone.setDefault(TimeZone.getTimeZone(ZoneOffset.UTC))
    }
    

    【讨论】:

    • 虽然如果您坚持使用时区列的时间戳可能会出现复杂情况 - 如果它只是一个时间戳则很好。
    猜你喜欢
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多