JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

JPA Annotations – @Temporal

March 6, 2014 by Krishna Srinivasan Leave a Comment

@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,

[code lang=”java”]
@Temporal(value)</span>
[/code]

The valid values are,

  • TemporalType.DATE
  • TemporalType.TIME
  • TemporalType.TIMESTAMP

@Temporal Example

For example,

[code lang=”java”]
@Temporal(TemporalType.DATE)
@Column(name = "DATE_COL")
private java.util.Date dateCol;
[/code]

The corresponding XML declaration is:

[code]
<entity class="Country">
<attributes>
<basic name="submitDate">
<temporal>DATE</temporal>
</basic>
</attributes>
</entity>
[/code]

It is equivalent of

  • DATE – equivalent of java.sql.Date
  • TIME – equivalent of java.sql.Time
  • TIMESTAMP – equivalent of java.sql.Timestamp

Filed Under: Java EE Tagged With: JPA, JPA 2 Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved