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());
3 comments :
Thank you for the feedback. I am glad this was useful.
Hi,
Can the update functionality be achieved by using LocalDateTime(from java.time)
Sure it is.
Post a Comment