本文介绍了在Spring Boot中正确利用JPA的时代秒数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想将数据库中的所有日期存储为纪元秒,以消除时区模糊性。我正在使用 例如,我有一个表格,其中创建列和修改列。很明显,我需要为添加的每个新条目提供 Created 列,并且我需要 Modified 我可以在我的表设计和注释中使用MySQL默认值的组合来完成此操作我的JPA实体,还是我需要实现一个 Repository 来处理这个创建/更新? 解决方案 code> public class MyEntity { @Column private long createdOn; public Date getCreatedOn(){ return new Date(createdOn); } public void setCreatedOn(Date createdOn){ this.createdOn = createdOn.getTime(); } } 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 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.For example, I have one table that has a Createdcolumn and a Modifiedcolumn. 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.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 Repository that takes care of this creation/updating for me? 解决方案 You can simply store the millis in a NUMERIC column while having the OOP side operate on Date objects:public class MyEntity { @Column private long createdOn; public Date getCreatedOn() { return new Date(createdOn); } public void setCreatedOn(Date createdOn) { this.createdOn = createdOn.getTime(); }} 这篇关于在Spring Boot中正确利用JPA的时代秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-25 01:29