8

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

setCloseDate(Timestamp closeDate)

However I do not want to generate/specify the timestamp from java code. Is there any way to annotate the attribute to indicate to use the current time at the database level when persisting the entity?

If not, what would be a good strategy for performing an update like this?

Under the covers the query I want to run is like this:

update CASE_FILE set CLOSE_DATE=CURRENT_TIMESTAMP where ID=1

I'm just getting started with JPA...so could be missing something fairly obvious here. Thanks!

  • This looks similar to http://stackoverflow.com/questions/221611/creation-timestamp-and-last-update-timestamp-with-hibernate-and-mysql . – Rick Aug 26 '09 at 15:16
  • 1
    It's similar to that question, but not the same since that question is about an always-updating last modified timestamp, whereas this is about storing a separate date field, but wanting it to be the current timestamp on the database server rather than on the application server. –  May 19 '14 at 18:36

1 Answers1

10

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

@Column(name = "timestamp", nullable = false, updatable = false, insertable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
public Timestamp timestamp;
atul singh
  • 664
  • 5
  • 11