38

(1) Why is the "@CreationTimestamp" field updated to null for a "save" called on the repository with a null value for that field? I expect that a field annotated with "@CreationTimestamp" is never updated and maintained only once at the time of creation. But it does not work that way in my current project.

(2) I had to include @Column(updatable =false) (in addition to @CreationTimestamp annotation). Why is this necessary?

Jothi
  • 511
  • 1
  • 5
  • 9

3 Answers3

34

It's 2020, hibernate-core-5.3.12, and still need to set updatable to false.

@CreationTimestamp      
@Column(updatable = false)
private LocalDateTime createdDate;

Update

I believe there will be no fix for this because this CreationTimestamp is from native hibernate package (org.hibernate.annotations), and I believe the efforts will be on the jpa abstraction (org.springframework.data.annotation.CreatedBy)

Guilherme Alencar
  • 1,243
  • 12
  • 21
  • 1
  • 1
    wow where can I find more about this issue or some more details on the fix? thanks! it worked – Tudor Dec 21 '20 at 10:21
  • 1
    I don't know if there is some fix in progress. I believe there isn't because this CreationTimestamp is from native hibernate package (`org.hibernate.annotations`), and I believe they are focusing their efforts on their jpa abstraction (`org.springframework.data.annotation.CreatedBy`). See https://springbootdev.com/2018/03/13/spring-data-jpa-auditing-with-createdby-createddate-lastmodifiedby-and-lastmodifieddate/ – Guilherme Alencar Dec 21 '20 at 15:39
  • 1
    It's quite amazing that almost all tutorials fail to capture this detail. – Harshil Sep 24 '21 at 16:06
  • It's 2022 and I still had to add this to overcome some random bug that happened when I added a column to the database table. Before this table change I made there were no problems at all with the `@CreationTimestamp` or `@Column` annotations. Hiberate v5.4. What a headache this was, thank you for saving my sanity. – Tanner Davis Nov 15 '22 at 23:53
3

@CreationTimestamp is not JPA but just Hibernate.

To create a field automatically with the creation date you can define a property like that:

  @Column(name = "creation_date", updatable = false)
  @Temporal(javax.persistence.TemporalType.TIMESTAMP)
  private Date creationDate = new Date();
Alessandro Polverini
  • 2,301
  • 19
  • 29
3

1) I faced the same issue on Spring Boot 2.1.2 which uses Hibernate 5.3.7. I read here that java.util.LocalDateTime handling was introduced in Hibernate 5.2.3, before that version you had to use one of the following date types:

  • java.util.Date
  • java.util.Calendar
  • java.sql.Date
  • java.sql.Time
  • java.sql.Timestamp

But it still did not work for me...

So I came up with javax.persistence.PrePersist annotation:

...

@Column(name = "created_at", updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;

...

@PrePersist
public void prePersistCreatedAt() {
    this.createdAt = new java.util.Date();
}

2) This post discusses that topic and gives decent explanation: Please explain about insertable=false and updatable=false in reference to the JPA @Column annotation.

Also pay attention to the not accepted posts, especially:

Defining insertable=false, updatable=false is useful when you need to map a field more than once in an entity...

kluckow
  • 181
  • 1
  • 4