We have your bog standard Java app under development, and a lot of the records we're creating (Hibernate entities in MySQL) have 'created' and 'modified' timestamps on them.
Now, me and one of the developers disagree - I believe that both of those fields should have a MySQL default of CURRENT_TIMESTAMP, and then the modified can be changed by the app. He wants both managed by the app.
Is there a compelling reason for either decision? I can't see why you'd want to add more explicit steps to the code, unless for some reason you were concerned about your servers (db, application) having inconsistent timestamps.
If none of these arguments apply, use the one that's easier for you (or the one that more developers vote for)
If you go for the application handling, either go for Pascal Thivent's suggestion with @PreUpdate
, or have your field set with a default value, like:
private Calendar date = Calendar.getInstance();
I would handle this from the code and use Hibernate/JPA's @PrePersist and @PreUpdate callbacks:
@PreUpdate
@PrePersist
public void setTimeStamps() {
modified = new Date();
if (created==null) {
created = new Date();
}
}
Main reason: portability (i.e. this will work with another database, for example in a testing context, without any database magic, no DEFAULT
, no trigger, nothing).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With