I have the below JPA Entity and I would like to use the @PrePersist and @PreUpdate to facilitate the setting of the lastUpdatedBy String with the current user from the Spring context, but I don't know how I can access that information from an entity level context. Any ideas?
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
}
}
But are you sure you can't do that in your `@Service` or `@Repository` in a less "hidden" way? – Luca Basso Ricci Aug 26 '13 at 21:48