Tôi đang gặp một số vấn đề về thừa kế và chú thích @PrePersist. mã nguồn của tôi trông giống như sau:@PrePersist với quyền thừa kế thực thể
_the 'cơ sở' lớp với updateDates chú thích() phương pháp:
@javax.persistence.Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Base implements Serializable{
...
@Id
@GeneratedValue
protected Long id;
...
@Column(nullable=false)
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
@Column(nullable=false)
@Temporal(TemporalType.TIMESTAMP)
private Date lastModificationDate;
...
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Date getLastModificationDate() {
return lastModificationDate;
}
public void setLastModificationDate(Date lastModificationDate) {
this.lastModificationDate = lastModificationDate;
}
...
@PrePersist
protected void updateDates() {
if (creationDate == null) {
creationDate = new Date();
}
lastModificationDate = new Date();
}
}
_ nay là 'con' lớp đó nên kế thừa toàn bộ các phương pháp "và chú thích " từ lớp cơ sở:
@javax.persistence.Entity
@NamedQueries({
@NamedQuery(name=Sensor.QUERY_FIND_ALL, query="SELECT s FROM Sensor s")
})
public class Sensor extends Entity {
...
// additional attributes
@Column(nullable=false)
protected String value;
...
// additional getters, setters
...
}
Nếu tôi lưu trữ/kiên trì thể hiện của lớp cơ sở để các cơ sở dữ liệu, mọi thứ đều hoạt động tốt. Các ngày đang được cập nhật. Nhưng bây giờ, nếu tôi muốn kéo dài một trường hợp trẻ em, cơ sở dữ liệu ném ngoại lệ sau đây:
MySQLIntegrityConstraintViolationException: Column 'CREATIONDATE' cannot be null
Vì vậy, theo ý kiến của tôi, điều này được gây ra bởi vì trong Child phương pháp "@PrePersist protected void updateDates()" không được gọi/được gọi trước khi kéo dài các cá thể vào cơ sở dữ liệu.
Có gì sai với mã của tôi?