0% found this document useful (0 votes)
86 views2 pages

Persist Creation and Update Timestamps With Hibernate

The @CreationTimestamp and @UpdateTimestamp annotations in Hibernate make it easy to track the timestamp of entity creation and last updates. For @CreationTimestamp, Hibernate sets the current timestamp when a new entity is persisted. For @UpdateTimestamp, Hibernate sets the current timestamp on every SQL update. These annotations can be added to date/time attributes like LocalDateTime to automatically populate the timestamps.

Uploaded by

Adolf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views2 pages

Persist Creation and Update Timestamps With Hibernate

The @CreationTimestamp and @UpdateTimestamp annotations in Hibernate make it easy to track the timestamp of entity creation and last updates. For @CreationTimestamp, Hibernate sets the current timestamp when a new entity is persisted. For @UpdateTimestamp, Hibernate sets the current timestamp on every SQL update. These annotations can be added to date/time attributes like LocalDateTime to automatically populate the timestamps.

Uploaded by

Adolf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Persist creation and update timestamps

@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. The only thing you have to do is to add the
annotation to an entity attribute.
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)
 java.util.Date
 java.util.Calendar
 java.sql.Date
 java.sql.Time
 java.sql.Timestamp

www.thoughts-on-java.org
Persist creation and update timestamps
Example
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.

@Entity
public class MyEntity {

@Column
@CreationTimestamp
private LocalDateTime createDateTime;

@Column
@UpdateTimestamp
private LocalDateTime updateDateTime;


}

www.thoughts-on-java.org

You might also like