How to autogenerate created or modified timestamp field?

后端 未结 8 1429
独厮守ぢ
独厮守ぢ 2020-12-05 03:11

My entity class:

@Entity
@Table(name = \"user\")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
           


        
8条回答
  •  再見小時候
    2020-12-05 03:30

    You can just create a new Date() whenever your instance is created, and then update the updated field whenever the entity gets updated:

    private Date created = new Date();
    private Date updated = new Date();
    
    @PreUpdate
    public void setLastUpdate() {  this.updated = new Date(); }
    

    Don't provide a setter for any of these methods, only getters.

提交回复
热议问题