Storing the creation timestamp or the timestamp of the last update is a common requirement for modern applications. It sounds like a simple requirement, but for a huge application, you don’t want to set a new update timestamp in every use case that changes the entity.
Don’t want to read? You can watch it here!
You need a simple, fail-safe solution that automatically updates the timestamp for each and every change. As so often, there are multiple ways to achieve that:
- You can use a database update trigger that performs the change on a database level. Most DBAs will suggest this approach because it’s easy to implement on a database level. But Hibernate needs to perform an additional query to retrieve the generated values from the database.
- You can use an entity lifecycle event to update the timestamp attribute of the entity before Hibernate performs the update.
- You can use an additional framework, like Hibernate Envers, to write an audit log and get the update timestamp from there.
- You can use the Hibernate-specific @CreationTimestamp and @UpdateTimestamp annotations and let Hibernate trigger the required updates.
It’s obvious that the last option is the easiest one to implement if you can use Hibernate-specific features. So let’s have a more detailed look at it.
@CreationTimestamp and @UpdateTimestamp
Hibernate’s @CreationTimestamp and @UpdateTimestamp annotations make it easy to track the timestamp of the creation and last update of an entity.
When a new entity gets persisted, Hibernate gets the current timestamp from the VM and sets it as the value of the attribute annotated with @CreationTimestamp. After that, Hibernate will not change the value of this attribute.
The value of the attribute annotated with @UpdateTimestamp gets changed in a similar way with every SQL Update statement. Hibernate gets the current timestamp from the VM and sets it as the update timestamp on the SQL Update statement.
Supported attribute types
You can use the @CreationTimestamp and @UpdateTimestamp with the following attribute types:
- java.time.LocalDate (since Hibernate 5.2.3)
- java.time.LocalDateTime (since Hibernate 5.2.3)
Let’s have a look at an example entity that uses the 2 annotations to store the timestamp of its creation and last update.
As you can see in the following code snippet, I just added the @CreationTimestamp annotation to the createDateTime attribute and the @UpdateTimestamp annotation to the updateDateTime attribute.
When you persist a new MyEntity, Hibernate will get the current time from the VM and store it as the creation and update timestamp. As you can see in the log output, Hibernate gets a new timestamp for each attribute. The creation and update timestamp will therefore not be the same even if the entity was never updated.
Hibernate will change the update timestamp with each SQL Update statement and keep the creation timestamp unchanged. But you might be surprised, when you see the generated SQL Update statement. It also updates the creation timestamp and sets it to its initial value.
Tracking the creation and last update timestamp of a database record is a common requirement. As you’ve seen, Hibernate’s @CreationTimestamp and @UpdateTimestamp annotations make it easy to implement. You just have to add an annotation to an entity attribute and Hibernate will take care of the necessary updates.