慕斯卡3215842
利用本文中的资源以及从不同来源获得的左右信息,我提出了这个优雅的解决方案,创建了以下抽象类import java.util.Date;import javax.persistence.Column;import javax.persistence.MappedSuperclass;import javax.persistence.PrePersist;import javax.persistence.PreUpdate;import javax.persistence.Temporal;import javax.persistence.TemporalType;@MappedSuperclasspublic abstract class AbstractTimestampEntity { @Temporal(TemporalType.TIMESTAMP) @Column(name = "created", nullable = false) private Date created; @Temporal(TemporalType.TIMESTAMP) @Column(name = "updated", nullable = false) private Date updated; @PrePersist protected void onCreate() { updated = created = new Date(); } @PreUpdate protected void onUpdate() { updated = new Date(); }}并让您的所有实体对其进行扩展,例如:@Entity@Table(name = "campaign")public class Campaign extends AbstractTimestampEntity implements Serializable {...}