Hibernate JPA @CreationTimestamp Example

@CreationTimestamp

The @CreationTimestamp annotation is used to specify that the currently annotated temporal type must be initialized with the current JVM timestamp value.

Marks a property as the creation timestamp of the containing entity. The property value will be set to the current VM date exactly once when saving the owning entity for the first time.
Supported property types:

Date
Calendar
Date
Time
Timestamp
Instant
LocalDate
LocalDateTime
LocalTime
MonthDay
OffsetDateTime
OffsetTime
Year
YearMonth
ZonedDateTime

@CreationTimestamp annotation

The @CreationTimestamp annotation instructs Hibernate to set the annotated entity attribute with the current timestamp value of the JVM when the entity is being persisted.

The supported property types are:

java.util.Date

java.util.Calendar

java.sql.Date

java.sql.Time

java.sql.Timestamp

Example : @CreationTimestamp mapping example

@Entity(name = "Event")
public static class Event {

@Id
@GeneratedValue
private Long id;

@Column(name = "`timestamp`")
@CreationTimestamp
private Date timestamp;

//Constructors, getters, and setters are omitted for brevity
}

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations