Saturday, September 17, 2016

TIMESTAMP implementations with Hibernate JPA

Definition of the TIMESTAMP field may be done in different ways and it depends on a particular use case which method is more appropriate. The post gives several examples of TIMESTAMP implementation, but it does not pretend to explain all the options.

The examples below are the use cases, that were met and implemented during my experience in real projects.
DB schema definition in the examples is done with the MySQL dialect and may differ for other dialects.

Immutable TIMESTAMP generated by DB on record creation


Generating immutable TIMESTAMP value by DB upon record creation is a very common case. Setting of a TIMESTAMP value is fully under DB responsibility and is reached with the DB schema definition. Java implementation includes only JPA mapping and counts on generating a value by a database.

DB schema:
 `CREATION_TIME` datetime NULL DEFAULT CURRENT_TIMESTAMP,
JPA mapping:
 private Date creationTime;
 ...
 @Column(name="CREATION_TIME", insertable = false,  updatable=false) 
 public Date getCreationTime() {  
      return creationTime;  
 }  

Mutable TIMESTAMP generated by DB on record update


Updating of TIMESTAMP value by DB upon record update is also rather common case. Similar to the above case of generating creation time, this is reached with the DB schema definition.

DB schema:
 `UPDATE_TIME` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
JPA mapping:
 private Date updateTime;
 ...
 @Column(name="UPDATE_TIME", insertable = false, updatable = false)
 public Date getUpdateTime() {
     return updateTime;
 } 

Combination of immutable and mutable TIMESTAMPs generated by DB


This case is a combination the above cases: DB is responsible to generate CREATE_TIME column value upon a record creation and update UPDATE_TIME value upon each record update. The value of the UPDATE_TIME column should be NULL in a new record ; it should be set to the current time upon a record update.
The solution again is reached with the DB schema definition and appropriate JPA mapping.

DB schema:
 `CREATION_TIME` datetime NULL DEFAULT CURRENT_TIMESTAMP,
 `UPDATE_TIME` datetime NULL on update CURRENT_TIMESTAMP,
JPA mapping:
 private Date creationTime;
 private Date updateTime;
 ...
 @Column(name="CREATION_TIME", insertable = false,  updatable=false)
 public Date getCreationTime() {
     return creationTime;
 }
 @Column(name="UPDATE_TIME", columnDefinition = "DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
 @Generated(GenerationTime.ALWAYS)
 public Date getUpdateTime() {
     return updateTime;
 } 

Setting of TIMESTAMP by Java


In this case all the work should be explicitly implemented in Java.

DB schema:
 `START_TIME` datetime NULL,
 `END_TIME` datetime NULL,
JPA mapping:
 private Date startTime;
 private Date endTime;
 ...
 @Column(name="START_TIME")
 public Date getStartTime() {
     return startTime;
 }
 @Column(name="END_TIME")
 public Date getEndTime() {
     return endTime;
 }
JPA entity methods to be called for setting time values:
 public void setStartTime(Date startTime) {
     this.startTime = startTime;
 }
 public void setEndTime(Date endTime) {
     this.endTime = endTime;
 }
Update start time with the current time:
 entity.setStartTime(new Date());

2 comments :

Kanye Co Jamila said...

Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Hibernate Training in Chennai . or learn thru Online Training mode Hibernate Online Training | Java EE Online Training. Nowadays Hibernate ORM has tons of job opportunities on various vertical industry.

Ala Schneider said...

Thank you for the feedback. I am glad this was useful.

About the author

My Photo
I trust only simple code and believe that code should be handsome. This is not a matter of technology, but professional approach, consolidated after years of software development. I enjoy to cause things working and feel very happy, when I manage to solve a problem.
Back to Top