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,
1
@Temporal(value)</span>
The valid values are,
TemporalType.DATE
TemporalType.TIME
TemporalType.TIMESTAMP
@Temporal Example
For example,
- See more at: http://www.javabeat.net/jpa-annotations-temporal/#sthash.d7vveKsL.dpuf
@Temporal(TemporalType.DATE)
2
@Column(name = "DATE_COL")
3
privatejava.util.Date dateCol;
The corresponding XML declaration is:
1
<entity class="Country">
2
<attributes>
3
<basic name="submitDate">
4
<temporal>DATE</temporal>
5
</basic>
6
</attributes>
7
</entity>
It is equivalent of
DATE – equivalent of java.sql.Date
TIME – equivalent of java.sql.Time
TIMESTAMP – equivalent of java.sql.Timestamp
- See more at: http://www.javabeat.net/jpa-annotations-temporal/#sthash.d7vveKsL.dpuf