如果我们想在 Hibernate 中存储 Date,是否需要 TemporalType = Timestamp?
Is TemporalType = Timestamp necessary if we want to store Date in Hibernate?
如果要在Hibernate中存储Date
,是否需要给@Temporal
?
@Column(name = "Date_Open")
@Temporal(TemporalType.TIMESTAMP)
public Date getDateOpen() {
return this.dateOpen;
}
如果我们不使用 @Temporal
注释,默认值 TemporalType
是什么?
如果不给@Temporal
默认值是多少,如果给TemporalType.Timestamp
有什么好处?
@Temporal 不需要与Date filed 一起使用,默认情况下hibernate 会使用Timestamp 保存日期,我们可以使用@Temporal 注解来指定hibernate 保存日期、时间或时间戳的精度。在普通 Java API 中,时间的时间精度没有定义。在处理时态数据时,您可能想要描述数据库中的预期精度。
以下 Type 用于表示 java.util.Date 的特定映射
或 java.util.Calenda.
TemporalType.DATE: maps the date as java.sql.Date.
TemporalType.TIME: maps the date as java.sql.Time.
TemporalType.TIMESTAMP: maps the date as java.sql.Timestamp.
Here 是关于@temporal
的很好的解释
还有关于日期字段中缺少@Temporal 注释的问题:
In plain Java APIs, the temporal precision of time is not defined.
When dealing with temporal data you might want to describe the
expected precision in database. Temporal data can have DATE, TIME, or
TIMESTAMP precision (ie the actual date, only the time, or both). Use
the @Temporal annotation to fine tune that.
From 2.2.2.1. Declaring basic property mappings.
This might indicate that the actual date representation in the
database is not defined and to be sure it is better to specify it
directly.
如果要在Hibernate中存储Date
,是否需要给@Temporal
?
@Column(name = "Date_Open")
@Temporal(TemporalType.TIMESTAMP)
public Date getDateOpen() {
return this.dateOpen;
}
如果我们不使用 @Temporal
注释,默认值 TemporalType
是什么?
如果不给@Temporal
默认值是多少,如果给TemporalType.Timestamp
有什么好处?
@Temporal 不需要与Date filed 一起使用,默认情况下hibernate 会使用Timestamp 保存日期,我们可以使用@Temporal 注解来指定hibernate 保存日期、时间或时间戳的精度。在普通 Java API 中,时间的时间精度没有定义。在处理时态数据时,您可能想要描述数据库中的预期精度。 以下 Type 用于表示 java.util.Date 的特定映射 或 java.util.Calenda.
TemporalType.DATE: maps the date as java.sql.Date.
TemporalType.TIME: maps the date as java.sql.Time.
TemporalType.TIMESTAMP: maps the date as java.sql.Timestamp.
Here 是关于@temporal
的很好的解释还有关于日期字段中缺少@Temporal 注释的问题:
In plain Java APIs, the temporal precision of time is not defined. When dealing with temporal data you might want to describe the expected precision in database. Temporal data can have DATE, TIME, or TIMESTAMP precision (ie the actual date, only the time, or both). Use the @Temporal annotation to fine tune that.
From 2.2.2.1. Declaring basic property mappings.
This might indicate that the actual date representation in the database is not defined and to be sure it is better to specify it directly.