@Temporal annotation in JPA implementation can only be used with the fields and property get methods. Also, this annotation can be specified for the persistent fields or properties java.util.Data or java.util.Calendar. This annotation is available since the release of JPA 1.0. @Temporal solves the one of the major issue of converting the date and time values from Java object to compatible database type and retrieving back to the application. When Java class declares the fields java.sql.Date or java.sql.Time, then it is compatible with the database types and will not have any issues. But, when we are using the java.util.Date or java.util.Calendar, then we have to specify Temporal types to map the appropriate database types when persisting to the database.
@Temporal Syntax
It can be declared with the following syntax,
@Temporal(value)</span>
The valid values are,
- TemporalType.DATE
- TemporalType.TIME
- TemporalType.TIMESTAMP
@Temporal Example
For example,
@Temporal(TemporalType.DATE) @Column(name = "DATE_COL") private java.util.Date dateCol;
The corresponding XML declaration is:
<entity class="Country"> <attributes> <basic name="submitDate"> <temporal>DATE</temporal> </basic> </attributes> </entity>
It is equivalent of
- DATE – equivalent of java.sql.Date
- TIME – equivalent of java.sql.Time
- TIMESTAMP – equivalent of java.sql.Timestamp