2010-03-31 39 views
7

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?

Trả lời

5

Tôi đã kiểm tra mã của bạn với Hibernate là nhà cung cấp JPA (và HSQLDB). Tôi chỉ thực hiện thay đổi sau trong lớp cơ sở (vì bạn không thể sử dụng IDENTIY - mặc định với HSQLDB và cũng với MySQL nếu tôi không sai - với một chiến lược TABLE_PER_CLASS):

@Id 
@GeneratedValue(strategy = GenerationType.TABLE) 
protected Long id; 

Với thay đổi này, phương pháp thử nghiệm sau đây vượt qua:

@Test 
public void test_Insert_EntityWithInheritedPrePersist() { 
    EntityWithInheritedPrePersist child = new EntityWithInheritedPrePersist(); 
    child.setValue("test"); 
    entityManager.persist(child); 

    assertEquals(child.getId(), Long.valueOf(1l)); 
    assertNotNull(child.getCreationDate()); 
    assertNotNull(child.getLastModificationDate()); 
} 

Vì vậy, phương thức chú thích được gọi trong lớp được kế thừa.

Điều này đặt ra câu hỏi: nhà cung cấp JPA nào bạn sử dụng?

xem this thread để biết thông tin cơ bản về điều này.

Các vấn đề liên quan