gpt4 book ai didi

java - 实体更新后@Createdby @CreatedDate 为空

转载 作者:行者123 更新时间:2023-12-04 12:36:36 35 4
gpt4 key购买 nike

我已经在 Spring 应用程序中实现了审计列,当我创建一个新实体时,列被设置,但当我更新实体时,我发现两个 @Createdby 和 @CreatedDate 设置为空,我该如何制作这些创建后列不可更改:

@MappedSuperclass
public abstract class Audit implements Serializable {

@CreatedBy
private String createdBy;

@CreatedDate
private Date createdDate;

@LastModifiedBy
private String lastModifiedBy;

@LastModifiedDate
private Date lastModifiedDate;
}

最佳答案

对我来说它有效:

@MappedSuperclass
public class Auditor {

@Column(name = "created_date", updatable = false)
@CreatedDate
private long createdDate;

@Column(name = "modified_date")
@LastModifiedDate
private long modifiedDate;

@Column(name = "created_by", updatable = false)
@CreatedBy
private String createdBy;

@Column(name = "modified_by")
@LastModifiedBy
private String modifiedBy;
}
并扩展类:
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Material extends Auditor {

@Id
@SequenceGenerator(name = "seq_material", sequenceName = "seq_material")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_material")
private Long id;

@Column
@NotBlank(message = "Nome é um campo obrigatório!.", groups = validacaoProd.class)
private String nome;

More...
}
我更新了数据库中的记录:
update material set created_date = 1618418750189, modified_date = 1618418750189 

关于java - 实体更新后@Createdby @CreatedDate 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64140558/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com