Java Hibernate中@Temporal注解有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25333711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:07:17  来源:igfitidea点击:

What is the use of the @Temporal annotation in Hibernate?

javahibernate

提问by Chaitanya

The Hibernate Documentation has the information below for the @Temporalannotation:

Hibernate 文档包含以下@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.

在普通 Java API 中,没有定义时间的时间精度。在处理时态数据时,您可能希望描述数据库中的预期精度。时态数据可以具有 DATE、TIME 或 TIMESTAMP 精度(即实际日期、仅时间或两者)。使用@Temporal 注释来微调它。

What does temporal precision of time is not definedmean? What is temporaldata and its precision? How does it fine tune?

什么temporal precision of time is not defined意思?什么是temporal数据及其精度?它是如何微调的?

采纳答案by Ankur Singhal

This annotation must be specified for persistent fields or properties of type java.util.Dateand java.util.Calendar. It may only be specified for fields or properties of these types.

此标注必须为持久化字段或类型的属性来指定java.util.Datejava.util.Calendar。只能为这些类型的字段或属性指定它。

The Temporalannotation may be used in conjunction with the Basicannotation, the Idannotation, or the ElementCollectionannotation (when the element collection value is of such a temporal type.

Temporal注释可与结合使用Basic的注释,所述Id注释,或ElementCollection注释(当元素集合值是这样一个时间类型。

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 (i.e., the actual date, only the time, or both). Use the @Temporalannotation to fine tune that.

在普通 Java API 中,没有定义时间的时间精度。在处理时态数据时,您可能希望描述数据库中的预期精度。时态数据可以具有 DATE、TIME 或 TIMESTAMP 精度(即,实际日期、仅时间或两者)。使用@Temporal注释对其进行微调。

The temporal data is the data related to time. For example, in a content management system, the creation-date and last-updated date of an article are temporal data. In some cases, temporal data needs precision and you want to store precise date/time or both (TIMESTAMP) in database table.

时间数据是与时间相关的数据。例如,在内容管理系统中,文章的创建日期和最后更新日期是时间数据。在某些情况下,时态数据需要精度并且您希望TIMESTAMP在数据库表中存储精确的日期/时间或两者 ( )。

The temporal precision is not specified in core Java APIs. @Temporalis a JPAannotation that converts back and forth between timestamp and java.util.Date. It also converts time-stampinto time. For example, in the snippet below, @Temporal(TemporalType.DATE)drops the time value and only preserves the date.

核心 Java API 中未指定时间精度。@Temporal是一个JPA在时间戳和java.util.Date. 它也转化time-stamp为时间。例如,在下面的代码段中,@Temporal(TemporalType.DATE)删除时间值并只保留日期

@Temporal(TemporalType.DATE)
private java.util.Date creationDate;

As per javadocs,

根据javadocs,

Annotation to declare an appropriate {@code TemporalType} on query method parameters. Note that this annotation can only be used on parameters of type {@link Date} with default TemporalType.DATE

在查询方法参数上声明适当的 {@code TemporalType} 的注释。请注意,此注释只能用于 {@link Date} 类型的参数,默认情况下TemporalType.DATE

[Information above collected from various sources]

[以上信息收集自各种来源]

回答by Ganesh Giri

use this

用这个

@Temporal(TemporalType.TIMESTAMP)
@Column(name="create_date")
private Calendar createDate;

public Calendar getCreateDate() {
    return createDate;
}

public void setCreateDate(Calendar createDate) {
    this.createDate = createDate;
}

回答by Montaser Mahadi

Temporal types are the set of time-based types that can be used in persistent state mappings.

时间类型是一组可用于持久状态映射的基于时间的类型。

The list of supported temporal types includes the three java.sqltypes java.sql.Date, java.sql.Time, and java.sql.Timestamp, and it includes the two java.utiltypes java.util.Dateand java.util.Calendar.

支持时间类型的列表包括三个java.sql类型java.sql.Datejava.sql.Time以及java.sql.Timestamp,它包括两种java.util类型java.util.Datejava.util.Calendar

The java.sqltypes are completely hassle-free. They act just like any other simple mapping type and do not need any special consideration.

这些java.sql类型完全没有麻烦。它们就像任何其他简单的映射类型一样,不需要任何特殊考虑。

The two java.utiltypes need additional metadata, however, to indicate which of the JDBC java.sqltypes to use when communicating with the JDBC driver. This is done by annotating them with the @Temporalannotation and specifying the JDBC type as a value of the TemporalTypeenumerated type.

java.util然而,这两种类型需要额外的元数据来指示java.sql在与 JDBC 驱动程序通信时使用哪种 JDBC类型。这是通过使用注释对它们进行@Temporal注释并将 JDBC 类型指定为TemporalType枚举类型的值来完成的。

There are three enumerated values of DATE, TIME, and TIMESTAMP to represent each of the java.sqltypes.

有 DATE、TIME 和 TIMESTAMP 三个枚举值来表示每种java.sql类型。

回答by Avikool91

@Temporalis a JPA annotation which can be used to store in the database table on of the following column items:

@Temporal是一个 JPA 注释,可用于存储在数据库表中的以下列项目:

  1. DATE(java.sql.Date)
  2. TIME(java.sql.Time)
  3. TIMESTAMP(java.sql.Timestamp)
  1. 日期( java.sql.Date)
  2. 时间( java.sql.Time)
  3. 时间戳( java.sql.Timestamp)

Generally when we declare a Datefield in the class and try to store it.
It will store as TIMESTAMPin the database.

通常当我们Date在类中声明一个字段并尝试存储它时。
它将作为TIMESTAMP存储在数据库中。

@Temporal
private Date joinedDate;

Above code will store value looks like 08-07-17 04:33:35.870000000 PM

上面的代码将存储值看起来像08-07-17 04:33:35.870000000 PM

If we want to store only the DATEin the database,
We can use/define TemporalType.

如果我们只想在数据库中存储DATE
我们可以使用 /define TemporalType

@Temporal(TemporalType.DATE)
private Date joinedDate;

This time, it would store 08-07-17in database

这次,它将在数据库中存储08-07-17

There are some other attributes as well as @Temporalwhich can be used based on the requirement.

还有一些其他属性以及@Temporal可以根据要求使用的属性。

回答by skryvets

If you're looking for short answer:

如果您正在寻找简短的答案:

In the case of using java.util.Date, Java doesn't really know how to directly relate to SQL types. This is when @Temporalcomes into play. It's used to specify the desired SQL type.

在使用 java.util.Date 的情况下,Java 并不真正知道如何与 SQL 类型直接关联。这就是@Temporal发挥作用的时候。它用于指定所需的 SQL 类型。

Source: Baeldung

资料来源:Baeldung

回答by Arun Raaj

I use Hibernate 5.2 and @Temporalis not required anymore.
java.util.date, sql.date, time.LocalDateare stored into DB with appropriate datatype as Date/timestamp.

我使用 Hibernate 5.2,@Temporal不再需要。
java.util.datesql.datetime.LocalDate以适当的数据类型作为日期/时间戳存储到数据库中。