Last update timestamp with JPA

后端 未结 3 1059
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 08:55

I\'m playing around a bit with JPA(Eclipselink to be specific). The below entity have a timestamp that\'s supposed to reflect whenever that entity was last updated.

相关标签:
3条回答
  • 2020-12-15 09:08

    Actually, surajz answer works. Consider a scenario where you are tracking 2 timestamps. You want to manually update them via the entity. You'd need to set one to updateble=false (the one you do not want updated when the record is updated), and leave the other free for updates.

    That's what worked for me.

    0 讨论(0)
  • 2020-12-15 09:12

    if you are using mysql, I think you can do the following to disable the timestamps from being updated from entity

    @Column(name = "lastUpdate", updatable= false, insertable=false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastUpdate;
    

    for oracle you time have to set the timestamps from the entity using @PreUpdate annotation as explained above.

    0 讨论(0)
  • 2020-12-15 09:27

    Use @PrePersist and @PreUpdate annotations and write your own event listener.

    Take a look at this answer for details. It's tagged as Hibernate but is applicable to any JPA provider.

    0 讨论(0)
提交回复
热议问题