简体   繁体   English

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

[英]What is the use of the @Temporal annotation in Hibernate?

The Hibernate Documentation has the information below for the @Temporal annotation: Hibernate 文档包含以下有关@Temporal注释的信息:

In plain Java APIs, the temporal precision of time is not defined.在普通的 Java API 中,没有定义时间的时间精度。 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).时态数据可以具有 DATE、TIME 或 TIMESTAMP 精度(即实际日期、仅时间或两者兼有)。 Use the @Temporal annotation to fine tune that.使用 @Temporal 注释对其进行微调。

What does temporal precision of time is not defined mean? temporal precision of time is not defined是什么意思? What is temporal data and its precision?什么是temporal数据及其精度? How does it fine tune?它是如何微调的?

This annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar .必须为java.util.Datejava.util.Calendar类型的持久字段或属性指定此注释。 It may only be specified for fields or properties of these types.只能为这些类型的字段或属性指定它。

The Temporal annotation may be used in conjunction with the Basic annotation, the Id annotation, or the ElementCollection annotation (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.在普通的 Java API 中,没有定义时间的时间精度。 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).时态数据可以具有 DATE、TIME 或 TIMESTAMP 精度(即实际日期、仅时间或两者兼有)。 Use the @Temporal annotation to fine tune that.使用@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.核心 Java API 中未指定时间精度。 @Temporal is a JPA annotation that converts back and forth between timestamp and java.util.Date . @Temporal是一个JPA注释,它在 timestamp 和java.util.Date之间来回转换。 It also converts time-stamp into time.它还将time-stamp转换为时间。 For example, in the snippet below, @Temporal(TemporalType.DATE) drops the time value and only preserves the date .例如,在下面的代码片段中, @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.在查询方法参数上声明适当的 {@code TemporalType} 的注释。 Note that this annotation can only be used on parameters of type {@link Date} with default TemporalType.DATE请注意,此注释只能用于具有默认TemporalType.DATE的 {@link Date} 类型的参数

[Information above collected from various sources] [以上信息从各种来源收集]

@Temporal is 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 )日期java.sql.Date
  2. TIME ( java.sql.Time )时间java.sql.Time
  3. TIMESTAMP ( java.sql.Timestamp )时间戳java.sql.Timestamp

Generally when we declare a Date field in the class and try to store it.通常当我们在类中声明一个Date字段并尝试存储它时。
It will store as TIMESTAMP in the database.它将作为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 DATE in the database,如果我们只想在数据库中存储DATE
We can use/define TemporalType .我们可以使用/定义TemporalType

@Temporal(TemporalType.DATE)
private Date joinedDate;

This time, it would store 08-07-17 in database这一次,它将在数据库中存储08-07-17

There are some other attributes as well as @Temporal which can be used based on the requirement.还有一些其他属性以及@Temporal可以根据要求使用。

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.sql types java.sql.Date , java.sql.Time , and java.sql.Timestamp , and it includes the two java.util types java.util.Date and java.util.Calendar .支持的时间类型列表包括三个java.sql类型java.sql.Datejava.sql.Timejava.sql.Timestamp ,它包括两个java.util类型java.util.Datejava.util.Calendar . java.util.Calendar

The java.sql types are completely hassle-free. java.sql类型完全没有麻烦。 They act just like any other simple mapping type and do not need any special consideration.它们的行为与任何其他简单映射类型一样,不需要任何特殊考虑。

The two java.util types need additional metadata, however, to indicate which of the JDBC java.sql types to use when communicating with the JDBC driver.然而,这两种java.util类型需要额外的元数据,以指示在与 JDBC 驱动程序通信时使用哪种 JDBC java.sql类型。 This is done by annotating them with the @Temporal annotation and specifying the JDBC type as a value of the TemporalType enumerated type.这是通过使用@Temporal注释对它们进行注释并将 JDBC 类型指定为TemporalType枚举类型的值来完成的。

There are three enumerated values of DATE, TIME, and TIMESTAMP to represent each of the java.sql types.有 DATE、TIME 和 TIMESTAMP 三个枚举值来表示每个java.sql类型。

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;
}

I use Hibernate 5.2 and @Temporal is not required anymore.我使用 Hibernate 5.2 并且不再需要@Temporal
java.util.date , sql.date , time.LocalDate are stored into DB with appropriate datatype as Date/timestamp. java.util.datesql.datetime.LocalDate以适当的数据类型作为日期/时间戳存储到数据库中。

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.在使用 java.util.Date 的情况下,Java 并不真正知道如何直接关联 SQL 类型。 This is when @Temporal comes into play.这就是@Temporal发挥作用的时候。 It's used to specify the desired SQL type.它用于指定所需的 SQL 类型。

Source: Baeldung资料来源: Baeldung

We use @Temporal annotation to insert date, time or both in database table.Using TemporalType we can insert data, time or both int table.我们使用 @Temporal 注解在数据库表中插入日期、时间或两者。使用 TemporalType 我们可以插入数据、时间或两者都 int 表。

@Temporal(TemporalType.DATE) // insert date
@Temporal(TemporalType.TIME) // insert time
@Temporal(TemporalType.TIMESTAMP) // insert  both time and date.

Simply, @Temporal is a Hibernate JPA annotation which should only be set on a java.util.Date or java.util.Calendar.简单地说, @Temporal是一个 Hibernate JPA 注释,它只能在 java.util.Date 或 java.util.Calendar 上设置。 It helps to convert the date and time values from Java object to compatible database type and will retrieve it back to the application.它有助于将日期和时间值从 Java 对象转换为兼容的数据库类型,并将其检索回应用程序。

supports;支持;

@Temporal(TemporalType.TIMESTAMP)
@Temporal(TemporalType.DATE)
@Temporal(TemporalType.TIME)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM