使用Hibernate / Envers,如何:
答案 0 :(得分:0)
1)从修订版听众中,调用修订版的fixTimezone
方法,如下所示。
2)要将其设为LocalDateTime
,请使用getRevisionDate
方法,如下所示。
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());
}
...
}
相关的Hibernate问题: