如何保存Hibernate Envers修订版信息的UTC(而不是本地)时间戳? [英] How to save UTC (instead of local) timestamps for Hibernate Envers revision info?
本文介绍了如何保存Hibernate Envers修订版信息的UTC(而不是本地)时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用休眠/启用,如何:
Using Hibernate/Envers, how to:
- 保存Hibernate Envers修订版信息的UTC(而不是本地)时间戳吗?
- 是否将时间戳记作为LocalDateTime(Java 8)?
推荐答案
1)从修订版侦听器中,调用修订版的fixTimezone
方法,如下所示.
1) From the revision listener, call the fixTimezone
method of the revision, as shown below.
2)要使用LocalDateTime
的形式,请使用getRevisionDate
方法,如下所示.
2) To get it as LocalDateTime
use the getRevisionDate
method, as shown below.
public class MyRevisionListener
implements RevisionListener {
@Override
public void newRevision(Object revisionEntity) {
MyRevision revision = (MyRevision)revisionEntity;
revision.fixTimezone();
}
}
@Entity
@RevisionEntity (MyRevisionListener.class)
public class MyRevision
implements Serializable {
@Id
@GeneratedValue
@RevisionNumber
private long id;
@RevisionTimestamp
@Temporal (TemporalType.TIMESTAMP)
@Column (nullable = false)
private Date date;
private static final ZoneId ZONE_ID_UTC = ZoneId.of("UTC");
public void fixTimezone() {
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZONE_ID_UTC);
date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
@NotNull
public LocalDateTime getRevisionDate() {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
...
}
相关的休眠问题:
- https://hibernate.atlassian.net/browse/HHH-10828
- https://hibernate.atlassian.net/browse/HHH-10827
- https://hibernate.atlassian.net/browse/HHH-10496
- https://hibernate.atlassian.net/browse/HHH-10828
- https://hibernate.atlassian.net/browse/HHH-10827
- https://hibernate.atlassian.net/browse/HHH-10496
这篇关于如何保存Hibernate Envers修订版信息的UTC(而不是本地)时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文