2011-10-29 28 views
5

Folks!@EntityListener có hoạt động với @MappedSuperclass không?

Nếu tôi định nghĩa một lớp thực thể và chú thích nó với @MappedSuperclass@EntityListener, người nghe cũng có được gọi cho các sự kiện vòng đời trong một lớp con không?

Ví dụ:

@MappedSuperclass 
@EntityListeners(class=LastUpdateListener.class) 
public abstract class Animal { 
    @Id private Integer id; 
    private String name; 
    private Calendar dateOfBirth; 
    @Transient private int age; 
    private Date lastUpdate; 
    //getters and setters 

    /** 
    * Set my transient property at load time based on a calculation, 
    * note that a native Hibernate formula mapping is better for this purpose. 
    */ 
    @PostLoad 
    public void calculateAge() { 
     Calendar birth = new GregorianCalendar(); 
     birth.setTime(dateOfBirth); 
     Calendar now = new GregorianCalendar(); 
     now.setTime(new Date()); 
     int adjust = 0; 
     if (now.get(Calendar.DAY_OF_YEAR) - birth.get(Calendar.DAY_OF_YEAR) < 0) { 
      adjust = -1; 
     } 
     age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR) + adjust; 
    } 
} 

public class LastUpdateListener { 
    /** 
    * automatic property set before any database persistence 
    */ 
    @PreUpdate 
    @PrePersist 
    public void setLastUpdate(Cat o) { 
     o.setLastUpdate(new Date()); 
    } 
} 

Cảm ơn.

Trả lời

3

Có, phương thức được chú thích bằng @PostLoad trong siêu lớp được ánh xạ và phương thức nghe của thực thể của LastUpdateListener được gọi.

Điều như sự kiện vòng đời cho bản thân siêu lớp được ánh xạ thậm chí không tồn tại. Như thường lệ, nó áp dụng cho lớp con.

+1

Có thể nói tại thời điểm này đây là không thể thực hiện trong TopLink nhật thực do này [lỗi] [1] [1]: https://bugs.eclipse.org/bugs/show_bug.cgi?id=302194 "lỗi" – rekiem87

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