我有多個表,UPDATE_TIME和INSERT_TIME列必須由應用程序代碼管理。有沒有辦法避免在每個實體類中重複列定義和行爲?具體而言,我想以某種方式考慮列定義和@ PrePersist/@ PreUpdate方法。如何在許多實體中重複JPA列定義?
@Entity
@Table(name="THINGS")
public class Thing {
// ... other columns ...
@Column(name = "INSERT_TIME")
private Date insertTime;
@Column(name = "UPDATE_TIME")
private Date updateTime;
@PrePersist
protected void onCreate() {
insertTime = new Date();
}
@PreUpdate
protected void onUpdate() {
updateTime = new Date();
}
}
您是否嘗試使用[@MappedSuperclass](http://docs.oracle.com/javaee/7/api/)?然後你只要讓你的Thing類繼承MappedSuperclass。每個JPA文檔的 – Unda