Erik
Erik

Reputation: 12140

Hibernate "ON UPDATE CURRENT TIMESTAMP" column

In MySQL it is possible to make a TIMESTAMP row being updated on every update operation. Is there a way to realize this for a column with Hibernate and map it to a POJO property?

So that I have something like this:

@Column
private Date updated;

Upvotes: 1

Views: 3031

Answers (2)

axtavt
axtavt

Reputation: 242686

If you want to do it at the database side, you can specify a custom column definition (if schema is generated by Hibernate, otherwise you need to declare it in your schema as desired), and instruct Hibernate that this property is generated at the database side:

@Column(columnDefinition = "TIMESTAMP ON UPDATE CURRENT TIMESTAMP")
@Generated(GenerationTime.ALWAYS)
private Date updated; 

Alternatively, you can do it at the application side, as suggested by Jigar Joshi.

Upvotes: 5

Jigar Joshi
Jigar Joshi

Reputation: 240900

You can have this to accomplish this thing

@PreUpdate
protected void onUpdate() {
   updated = new Date();
}

Upvotes: 2

Related Questions