Datetime Java Hibernate Mapping

Datetime Java Hibernate Mapping

Once table got created in database next thing you will have to do is map table columns definition with Java object. If any of your table column definition has date and datetime columns defined then for hibernate please use below to map it:

  • To map datatime in Hibernate use columnDefinition attribute of the @Column annotation as below and use java.util.Date
@Column(name="updateTime")
@Temporal(TemporalType.TIMESTAMP)
private Date updateTime;

As you see above we have used @Temporal annotation which tells the Hibernate if it should use java.sql.Date or java.sql.Timestamp to store the date value read from the database. Both java.sql.Date and java.sql.Timestamp extends java.util.Date class but java.sql.Date does not hold any time information and only the date. Also please note that your attribute definition should be private.

  • If you want to map date directly to date object without timestamp please use java.sql.Date as below:
@Column(name="eventDate")
private java.sql.Date eventDate;

 

Reference:

Leave a Reply

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