10

I have an entity with fields

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "edit_timestamp", 
        columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
private Date editTimestamp;

@Version
@Column(name = "edit_count")
private short editCount;

private String text;

When I try to update with Spring-Data-JPA, I observe edit_count has been incremented, but edit_timestamp still remain the same. If I manually invoke SQL

UPDATE post SET TEXT='456' WHERE post_id=1;

the edit_timestamp is updated. If I add

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

it works w/o issue. My question is why w/o @PreUpdate the edit_timestamp is not updated?

1
  • 1
    To downvoter, please provide your comment... Sep 21, 2012 at 15:10

3 Answers 3

11

You need to change the column annotation to include updatable = false. This will cause the edit_timestamp column to not show up in the update SQL, so the JPA provider won't include the current value of the field which is what is causing it to override the default.

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "edit_timestamp", 
        updatable = false,
        columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
private Date editTimestamp;
3
  • This is partially work. With updatable = false, the edit_timestamp column not show up in the update query. From database, I observed the edit_timestamp has been updated. However, the getter for editTimestamp is not aware of the updated value, as it is not triggered by @PreUpdate, so it still returns the old value. Jun 6, 2013 at 9:54
  • Hi I wanted to know if I can set current timestamp on create but on update I want it to remain the same.
    – user6439492
    Dec 19, 2019 at 11:46
  • @LeeCheeKiam How to fix this, simply remove updatable = false seems cannot work Feb 21, 2020 at 13:55
5

You can mark the variables as update or creation timestamps in your entity. In the following example you can see how this can be done very easily using hibernate annotations:

@UpdateTimestamp
private LocalDateTime editTimestamp;

@CreationTimestamp
private LocalDateTime creationTimestamp;

Just for the reference and to make sure there is no confusion, I am using the following package imports for the respective datatypes and annotations:

import org.hibernate.annotations.CreationTimestamp
import org.hibernate.annotations.UpdateTimestamp
import java.time.LocalDateTime
-1

Because in your initial set of annotations, all you've told it about the edit_timestamp column is that it is a timestamp; JPA doesn't know that it needs to update it. I'm guessing that when you're manually executing the SQL statement, you have some sort of on-update trigger that's changing those fields for you - but are being overwritten by the data coming from the persisted entity when you update it.
If you don't need the 'edited' count/timestamp, try removing them from the entity, and see if that works. Otherwise, you have a working solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.