3

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;
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • you can use @CreationTimestamp.. similar answer you can refer https://stackoverflow.com/questions/811845/setting-a-jpa-timestamp-column-to-be-generated-by-the-database – Rahul Sawant Aug 14 '21 at 10:23
  • You want to create database column with `CURRENT_TIMESTAMP` or want to store current timestamp in column from jpa ? – Eklavya Aug 14 '21 at 10:48
  • I want to store current timestampt default valie in jpa – expelliarmus Aug 14 '21 at 10:51

1 Answers1

1

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.

A good tutorial about Spring Data JPA : Auditing

Eklavya
  • 17,618
  • 4
  • 28
  • 57