JPA Annotations – @Temporal

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 private java.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


引用自:http://www.javabeat.net/jpa-annotations-temporal/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值