What is the use of @Temporal annotation in Hibernate?

The Hibernate documentation has the following information for @Temporal annotation:

In simple Java APIs, the time accuracy of time is not defined. When working with temporary data, you can expect the expected accuracy in the database. Temporary data can have DATE, TIME, or TIMESTAMP Accuracy (i.e., the actual date, only time, or both). use @Temporal annotation for fine tuning.

What does temporal precision of time is not defined mean? What is temporal data and its accuracy? How to set it up?

+77
java hibernate
Aug 15 '14 at 20:26
source share
6 answers

This annotation must be specified for persistent fields or properties like java.util.Date and java.util.Calendar . It can only be specified for fields or properties of these types.

The Temporal annotation can be used in combination with the Basic annotation, Id annotation, or ElementCollection annotation (when the value of the element set is of such a temporary type.

In simple Java APIs, the time accuracy of time is not defined. When working with temporary data, you can describe the expected accuracy in the database. Temporary data may have a DATE, TIME, or TIMESTAMP precision (i.e., Actual date, time only, or both). For fine tuning, use the @Temporal annotation.

Time data is time related data. For example, in a content management system, the creation date and the date of the last update of an article are temporary data. In some cases, the temporary data needs accuracy, and you want to save the exact date / time or both ( TIMESTAMP ) in the database table.

Temporary accuracy is not specified in the core Java APIs. @Temporal is a JPA annotation that converts back and forth between a timestamp and java.util.Date . It also converts time-stamp to time. For example, in the @Temporal(TemporalType.DATE) snippet below , the time value is reduced and only the date is saved .

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

According to javadocs,

Annotations for declaring the corresponding {@code TemporalType} request method parameters. Please note that this annotation can only be used for parameters like {@link Date} with the default TemporalType.DATE

[Information collected from various sources]

+82
Aug 16 '14 at 6:38
source share

@Temporal - JPA annotation that can be used to store the following column elements in a database table:

  1. DATE ( java.sql.Date )
  2. TIME ( java.sql.Time )
  3. TIMESTAMP ( java.sql.Timestamp )

Usually when we declare a Date field in a class and try to save it.
It will be stored as TIMESTAMP in the database.

 @Temporal private Date joinedDate; 

The above code will store the value looks like 08-07-17 04: 33: 35.870000000 PM

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

 @Temporal(TemporalType.DATE) private Date joinedDate; 

This time it will store 08-07-17 in the database

There are other attributes, as well as @Temporal which can be used depending on the requirements.

+21
Jul 08 '17 at 11:20
source share

Temporary types are a set of time-based types that can be used in constant state comparisons.

The list of supported time types includes three types java.sql java.sql.Date , java.sql.Time and java.sql.Timestamp , and it includes two types java.util java.util.Date and java.util.Calendar

The java.sql types are completely hassle-free. They act just like any other simple type of matching, and do not require special consideration.

The two java.util types require additional metadata to indicate which JDBC type java.sql use when communicating with the JDBC driver. This is done by annotating them using the @Temporal annotation and specifying the JDBC type as the value of the enumerated TemporalType type.

There are three enumerated values ​​for DATE, TIME, and TIMESTAMP for representing each of the java.sql types.

+8
Jul 03 '16 at 7:42 on
source share

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; } 
+3
May 09 '16 at 4:07
source share

I am using Hibernate 5.2 and @Temporal no longer required.
java.util.date, sql.date, time.LocalDate are stored in the database with the corresponding data type in the form of date / time.

+1
Feb 17 '19 at 20:35
source share

If you are looking for a short answer:

In the case of using java.util.Date, Java doesn't really know how to relate directly to the types of SQL. This is when @Temporal comes into play. Used to indicate the desired SQL type.

Source: Baeldung

0
Jan 12 '19 at 12:30
source share



All Articles