JPA set the TIMESTAMP field to CURRENT_TIMESTAMP when it is saved

I have a TIMESTAMP column in a table that is null when the record is initially inserted. I would like to update the value to the current time. My generated object has a given method for the field as such:

setCloseDate(Timestamp closeDate) 

However, I do not want to generate / specify a timestamp from java code. Is there a way to annotate an attribute to indicate the use of current time at the database level when saving an object?

If not, what is a good strategy to carry out such an update?

Under the covers, the query I want to run is as follows:

 update CASE_FILE set CLOSE_DATE=CURRENT_TIMESTAMP where ID=1 

I'm just getting started with JPA ... so there might be something pretty obvious here. Thanks!

+4
source share
1 answer

in your jpa entity class use the following code in the current timestamp column

 @Column(name = "timestamp", nullable = false, updatable = false, insertable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") public Timestamp timestamp; 
+5
source

All Articles