我正在玩JPA(
Eclipselink是具体的).以下实体有一个
每当该实体上次更新时,应该反映的时间戳.
每当该实体上次更新时,应该反映的时间戳.
每次自动更新JPA时间戳的策略是什么?
这个实体改变了?
如果我还想要一个“创建”时间戳,那么我该怎么办?只有当实体第一次被持久化时才设置,再也不能再被修改了?
@Entity
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String note;
public Item () {
}
@Id
@GeneratedValue(strategy=SEQUENCE)
@Column(unique=true,nullable=false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(length=255)
@Column(nullable=false)
public String getNote() {
return this.note;
}
public void setNote(String note) {
this.note = note;
}
@Column(nullable=false)
public Timestamp getUpdated() {
return this.updated;
}
public void setUpdated(Timestamp updated) {
this.updated = updated;
}
}
解决方法
使用
@PrePersist和
@PreUpdate注释,并编写自己的事件监听器.
看看this answer的细节.它被标记为Hibernate,但适用于任何JPA提供商.