Hi, I have a timestamp property on my entity which is supposed to be updated every time the entity is updated (and it has to be set on insert as well). A fairly typical edit audit stuff ...
Setting the timestamp is easy when done with JVM. I simply have a method marked with @PreUpdate/@PrePersist
Code:
@PrePersist
@PreUpdate
protected void updateAuditInfo() {
auditTimestamp = new Date();
}
This works, but what if I want the auditTimestamp to be set by the database using CURRENT TIMESTAMP? So, what would get inserted when the entity is persisted would be something like INSERT INTO MYTABLE(... fields ..., AUDIT_TS) VALUES (... field values..., CURRENT TIMESTAMP).
How would I achieve that? @Version is not an option as there is another (int) column that is the version. I tried using a custom user type by extending org.hibernate.usertype.UserType, but that doesn't work since you cannot set the literal value CURRENT TIMESTAMP on a PreparedStatement ... I also tried setting the timestamp property type to org.hibernate.type.DbTimestampType, that didn't help either.
I'm using Hibernate 3.2 with Annotations and the EntityManager,