expelliarmus
expelliarmus

Reputation: 47

How to set default value to CURRENT_TIMESTAMP in JPA?

I have created an entity and I want to give createdDate variable, the CURRENT_TIMESTAMP default value, how can I do that?

@Column(name = "created_date")
private LocalDateTime createdDate;

Upvotes: 4

Views: 588

Answers (1)

Eklavya
Eklavya

Reputation: 18430

You can use @CreatedDate with Spring Data JPA provided AuditingEntityListener

@EntityListeners(AuditingEntityListener.class)
class Entity {
   ...
   @CreatedDate
   @Column(name = "created_date")
   private LocalDateTime createdDate;
   ...
}

Then use @EnableJpaAuditing on your application main class to enable the Spring Data JPA Auditing features for the application.

Upvotes: 1

Related Questions