保存 LocalDate 到数据库

huangapple 未分类评论47阅读模式
英文:

Saving LocalDate to database

问题

I am pretty new to this and have a couple of questions:

  1. 如果我需要记录用户在界面上进行更改的日期和时间,是需要从界面传递那个日期,还是在请求到达后端时由后端创建一个日期和时间对象比较好?

  2. 如果我需要将 LocalDate 对象保存到数据库,以下方法是否可行?

     @Column(name = "last_preference_change_date")
     private LocalDateTime lastPreferenceChangeDate;
    

我的目标是记录用户更改特定设置的时刻,并将其存储在数据库中以便能够检索它,如果该用户例如恢复了这些更改,我希望现有的日期在数据库中被覆盖并保存新的日期。

英文:

I am pretty new to this and have a couple of questions:

  1. If i would need to record a date and time when user changed something in the UI, do i need to pass that date from the UI or is better for the back end to create a date and time object when the request reaches it?

  2. If i would need to save LocalDate object to a database , would the following approach work?

     @Column(name = "last_preference_change_date")
     private LocalDateTime lastPreferenceChangeDate;
    

My goal is to record a moment when user changed particular settings and to store that in db to be able to retrieve it, and if that user for example reverted those changes i would like my existing date to be overwritten in db and new one saved.

答案1

得分: 0

使用PrePersistPreUpdate回调/方法来创建一个可以修改每次数据更改的函数。这适用于任何JPA提供程序。

@PreUpdate
@PrePersist
public void updateModified() {
    modifiedTime = new Date();
    if (createdTime == null) {
      createdTime = new Date();
    }
}
英文:

Use PrePersist and PreUpdate callback/method to create function that can modify every data changed. It is applicable to any JPA provider.

@PreUpdate
@PrePersist
public void updateModified() {
    modifiedTime = new Date();
    if (createdTime == null) {
      createdTime = new Date();
    }
}

答案2

得分: 0

使用以下注解进行审计:

@CreatedDate
private LocalDateTime createdDate;
@LastModifiedDate
private LocalDateTime lastModifiedDate;
@LastModifiedBy
@CreatedBy
英文:

use below annotation for auditing

@CreatedDate
private LocalDateTime createdDate;
@LastModifiedDate
private LocalDateTime lastModifiedDate;
@LastModifiedBy
@CreatedBy

huangapple
  • 本文由 发表于 2020年7月27日 01:52:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/63103708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定