上次使用JPA更新时间戳

nos*_*nos 12 java jpa

我正在玩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;
    }


}
Run Code Online (Sandbox Code Playgroud)

Chs*_*y76 16

使用@PrePersist@PreUpdate注释并编写自己的事件监听器.

有关详细信息,请查看此答案.它被标记为Hibernate,但适用于任何JPA提供程序.