我是春天的新手,我很困惑@CreatedDate注释在一个实体中是如何工作的.
我做了一个谷歌搜索,有很多解决方案,但除了一个,它们都没有为我工作.我很困惑为什么?
这是我先试过的
@Entity
@EntityListeners(AuditingEntityListener.class)
public class User implements Serializable {
@Id
@GeneratedValue
private Long id;
private String name;
@CreatedDate
private Date created;
public User(String name) {
this.name = name;
}
public User() {
}
Run Code Online (Sandbox Code Playgroud)
那没起效.我在created列中的值为NULL .
然后我做了这个.
@Entity
@EntityListeners(AuditingEntityListener.class)
public class User implements Serializable {
@Id
@GeneratedValue
private Long id;
private String name;
@CreatedDate
private Date created = new Date();
public User(String name) {
this.name = name;
}
public User() {
}
Run Code Online (Sandbox Code Playgroud)
这实际上将时间戳存储在db中.我的问题是我遵循的大多数教程都表明我不需要new Date()获取当前时间戳.看起来我确实需要它.有什么我想念的吗?