我已经在Spring应用程序中实现了审计列,当我创建一个新实体时,它会起作用,但是设置了列,但是当我更新实体时,我发现两个@Createdby和@CreatedDate设置为null,创建后,我使这些列不可更改:
@MappedSuperclass
public abstract class Audit implements Serializable {
@CreatedBy
private String createdBy;
@CreatedDate
private Date createdDate;
@LastModifiedBy
private String lastModifiedBy;
@LastModifiedDate
private Date lastModifiedDate;
}
答案 0 :(得分:3)
对我来说它有效:
@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
答案 1 :(得分:0)
您可以在类级别使用@DynamicUpdate。它将仅考虑更新列,而忽略其他列以放入UPDATE查询。