Close

JPA - Persisting java.util.Date and java.util.Calender using @Temporal Annotation

[Updated: Jun 10, 2018, Created: Jun 10, 2018]

@Temporal annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar.

This annotation is defined as:

package javax.persistence;
  ........
public @interface Temporal {
   TemporalType value();
}

where TemporalType enum is defined as:

package javax.persistence;
...
public enum TemporalType {
     DATE,//Map as java.sql.Date
     TIME,//Map as java.sql.Time
     TIMESTAMP// Map as java.sql.Timestamp
}

Since java.util.Date and java.util.Calendar are not compatible to the standard SQL date types (DATE, TIME, TIMESTAMP), we must use @Temporal on these types and should specify the TemporalType for intended conversion.

Example

Entity

@Entity
public class ScheduledEvent {
  @Id
  @GeneratedValue
  private long id;
  private String eventName;

  @Temporal(TemporalType.DATE)
  private Date date;

  @Temporal(TemporalType.TIME)
  private Date start;

  @Temporal(TemporalType.TIME)
  private Date end;

  @Temporal(TemporalType.TIMESTAMP)
  private Date eventCreated;
    .............
}
public class ExampleMain {
  private static EntityManagerFactory entityManagerFactory =
          Persistence.createEntityManagerFactory("example-unit");

  public static void main(String[] args) {
      try {
          persistEvent();
          findEvent();
          nativeQuery("select CONCAT('start= ', e.start, '\nend= ', e.end, '\ndate= ', e.date,"
                   +"'\neventCreated= ', e.eventCreated)"
                  + " from ScheduledEvent e");
      } finally {
          entityManagerFactory.close();
      }
  }

  private static void persistEvent() {
      ScheduledEvent scheduledEvent = ScheduledEvent.create("Test event",
              LocalDate.of(2018, 6, 1),
              LocalTime.of(9, 30, 45),
              LocalTime.of(11, 45, 30));
      System.out.println("-- persisting entity --");
      System.out.println(scheduledEvent);
      EntityManager em = entityManagerFactory.createEntityManager();
      em.getTransaction().begin();
      em.persist(scheduledEvent);
      em.getTransaction().commit();
      em.close();
  }

  private static void findEvent() {
      System.out.println("-- finding entity --");
      EntityManager em = entityManagerFactory.createEntityManager();
      ScheduledEvent scheduledEvent = em.find(ScheduledEvent.class, 1L);
      System.out.println(scheduledEvent);
      em.close();
  }

  public static void nativeQuery(String s) {
      EntityManager em = entityManagerFactory.createEntityManager();
      System.out.println("-- native query --");
      Query query = em.createNativeQuery(s);
      List list = query.getResultList();
      for (Object o : list) {
          if (o instanceof Object[]) {
              System.out.println(Arrays.toString((Object[]) o));
          } else {
              System.out.println(o);
          }
      }
      em.close();
  }
}
-- persisting entity --
ScheduledEvent{id=0, eventName='Test event', date=Fri Jun 01 00:00:00 CDT 2018, start=Wed Dec 31 09:30:45 CST 2, end=Wed Dec 31 11:45:30 CST 2, eventCreated=Sun Jun 10 21:01:58 CDT 2018}
-- finding entity --
ScheduledEvent{id=1, eventName='Test event', date=2018-06-01, start=09:30:45, end=11:45:30, eventCreated=2018-06-10 21:01:58.265}
-- native query --
start= 09:30:45
end= 11:45:30
date= 2018-06-01
eventCreated= 2018-06-10 21:01:58.265

Example Project

Dependencies and Technologies Used:

  • h2 1.4.197: H2 Database Engine.
  • hibernate-core 5.2.13.Final: The core O/RM functionality as provided by Hibernate.
    Implements javax.persistence:javax.persistence-api version 2.1
  • JDK 1.8
  • Maven 3.3.9

@Temporal Annotation Example Select All Download
  • jpa-temporal-annotation
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • META-INF

See Also