问题描述
我所有的数据库表都应该有一个 endTime 字段,默认情况下应该是 END_OF_TIME 或类似的东西.我对 2038 的限制不满意,所以我希望 endTime 在 mysql 中是 DATETIME 类型.
All my db tables should have an endTime field which by default should be END_OF_TIME or something like that. I am not happy about the 2038 limitation so I want endTime to be of type DATETIME in mysql.
我的 Java 代码是:
My Java code is:
@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class BaseDBEntity {
@Id
@Column(length=36)
public String id;
@Temporal(TemporalType.TIMESTAMP)
public Date startTime;
@Temporal(TemporalType.TIMESTAMP)
public Date endTime;
public BaseDBEntity() {
}
}
我可以通过使用 DATETIME 类型的 endTime 字段手动创建表来解决这个问题,然后将实体 endTime 映射到该列,但是我希望 Hibernate 自动生成表 - 我该怎么做?
I can work around by creating the table manually with an endTime field of type DATETIME, and than map the entity endTime to that column, however I would like Hibernate to generate the tables automatically - how can I do that?
推荐答案
使用:
Use the columnDefinition
attribute of the @Column
annotation:
@Column(name = "startTime", columnDefinition="DATETIME")
@Temporal(TemporalType.TIMESTAMP)
private Date startTime;
请把你的属性设为私有.
And please, make your attributes private.
这篇关于如何使用 Hibernate 注释将 Java 日期映射到 mysql 中的 DATETIME(默认为 TIMESTAMP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!