Java – JPA last update timestamp

I'm playing JPA (eclipse link is specific) The following entities have one

What is the policy for automatically updating JPA timestamps each time? This entity has changed?

What should I do if I also want a "create" timestamp? It is set only when the entity is persisted for the first time and can no longer be modified?

@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;
    }


}

Solution

Use @ prepersist and @ preupdate comments and write your own event listener

Take a look at the details of this answer It is labeled hibernate, but applies to any JPA provider

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>