我有下面的 JPA 实体,我想使用 @PrePersist 和 @PreUpdate 来帮助在 Spring 上下文中使用当前用户设置 lastUpdatedBy 字符串,但我不知道如何从实体访问该信息水平上下文。有任何想法吗?
public abstract class BaseEntity {
private Date createdDate;
private String lastUpdatedBy;
@Column(name = "LAST_UPDATED_BY")
public String getLastUpdatedBy() {
return lastUpdatedBy;
}
public void setLastUpdatedBy(String lastUpdatedBy) {
this.lastUpdatedBy = lastUpdatedBy;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name="CREATED_DATE")
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date created) {
if (created != null) {
this.createdDate = created;
}
}
@PrePersist
protected void onCreate() {
createdDate = new Date();
//lastUpdatedBy = //need to access Spring user here
}
@PreUpdate
public void onUpdate() {
updatedDate = new Date();
//lastUpdatedBy = //need to access Spring user here
}
}