jpa 自动维护创建更新时间

@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 自动维护创建更新时间)