美文网首页
jpa 自动维护创建更新时间

jpa 自动维护创建更新时间

作者: hk_faith | 来源:发表于2019-09-25 17:47 被阅读0次
@Data
@Entity
@EntityListeners(AuditingEntityListener.class)
@ToString(exclude = {"notificationUserMapList"})
@Table(name = "notification")
public class Notification {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @CreatedDate
    @Column(name = "created_at")
    private Timestamp createdAt;

    @LastModifiedDate
    @Column(name = "updated_at")
    private Timestamp updatedAt;
}

备注:

  • 表的字段上加上 @CreatedDate @LastModifiedDate
  • @EntityListeners(AuditingEntityListener.class)
  • 启动类上加 @EnableJpaAuditing

Hibernate 也提供了类似上述时间注解的功能实现,这种方法只需要一步配置,更改为注解 @UpdateTimestamp@CreationTimestamp

相关文章

网友评论

      本文标题:jpa 自动维护创建更新时间

      本文链接:https://www.haomeiwen.com/subject/qxtbuctx.html