Field with @CreationTimestamp annotation is null while save on repository

大兔子大兔子 提交于 2020-01-31 07:58:07

问题


(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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!