在Spring Boot中正确利用JPA的时代秒数 [英] Properly Utilizing Epoch Seconds with JPA in Spring Boot
问题描述
我想将数据库中的所有日期存储为纪元秒,以消除时区模糊性。我正在使用 例如,我有一个表格,其中 我可以在我的表设计和注释中使用MySQL默认值的组合来完成此操作我的JPA实体,还是我需要实现一个 code> public class MyEntity { I would like to store all of the dates in my database as epoch seconds in order to eliminate timezone ambiguity. I am using the For example, I have one table that has a Can I accomplish this using a combination of MySQL defaults in my table design and annotations with my JPA entities, or do I need to implement a You can simply store the millis in a NUMERIC column while having the OOP side operate on Date objects:
这篇关于在Spring Boot中正确利用JPA的时代秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
创建
列和修改
列。很明显,我需要为添加的每个新条目提供 Created
列,并且我需要 Modified
Repository
来处理这个创建/更新? <>解决方案
@Column
private long createdOn;
public Date getCreatedOn(){
return new Date(createdOn);
}
public void setCreatedOn(Date createdOn){
this.createdOn = createdOn.getTime();
}
}
JpaRepository
interface provided through one of Spring Boot's starters (Data for JPA) to persist changes that I make to the entities that will store epoch seconds. However, I am having a little bit of trouble figuring out how to keep everything updated properly.Created
column and a Modified
column. As may be obvious, I need the Created
column to be provided with epoch seconds for each new entry that is added, and I need the Modified
column to be updated every time that an existing entry is changed.Repository
that takes care of this creation/updating for me?public class MyEntity {
@Column
private long createdOn;
public Date getCreatedOn() {
return new Date(createdOn);
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn.getTime();
}
}