问题
(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?
回答1:
@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();
来源:https://stackoverflow.com/questions/38227203/field-with-creationtimestamp-annotation-is-null-while-save-on-repository