(...) SYSDATE is inserted only once and not updated for subsequent updates.
First of all, let me make something clear: Generated
means that the field is generated by the database and that Hibernate needs to read it after insert/update to update the entity. Using default SYSDATE
in the column definition works fine for an INSERT but for an UPDATE, you'll need a trigger.
Also, lastUpdDTTM is not part of sql generated by hibernate.
Well, you told Hibernate that the field is ALWAYS
generated by the database so I'm not surpised that Hibernate doesn't include it in the generated SQL (actually, I believe that this somehow conflicts with udpatable = true
, I would expect Hibernate to complain about it).
Anyway, as I said, it's not Hibernate that will update this field, it's the database and you need a trigger, Hibernate will just refresh the entity after an update to get the new value.
A different approach would be to use callback annotations, for example for the last update date:
@PreUpdate
protected void updateDates() {
lastUpdDTTM = new Date();
}
For better consistency, you could even use the same approach for the creation date with @PrePersit
:
@PrePersist
@PreUpdate
protected void updateDates() {
Date now = new Date();
if (createdDTTM == null) {
createdDTTM = new Date(now.getTime());
}
lastUpdDTTM = now;
}