2012-04-11 80 views
0

我一直在工作的實體,一對多的關係,問題是,每當我用這個:Eclipse鏈接替代@PreUpdate註釋?

//Parent class  

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER, 
     targetEntity = FeedbackCategory.class, mappedBy = "parent")  
@PrivateOwned 
@OrderBy("category ASC") 
private List<Child> children; 

//... other code here, i.e. getters-setters 

@PrePersist 
@PreUpdate 
void prePersistUpdate() { 

    // set the foreign key of child to this.ID 
    if(children != null && !children.isEmpty()) 
    { 
     for(Child ch: children) 
     { 
      ch.setParent(this);  
     }   
    } 
} 

更新Parent.class時,尤其是當清潔更新實體,家長間沒有的ID」 t與子實體一起堅持(作爲外鍵)。請幫助...

看起來@PreUpdate不起作用,@PrePersist完全起作用。

回答

1

一般來說,最好是正確地維護你的模型。

添加addChild()方法,該方法添加子項並設置父項。那麼你的模特不會腐敗。

@PreUpdate稍後在提交過程中發生(在確定需要更新對象之後)。我不認爲有早期的JPA事件,但可以使用EclipseLink PreWrite事件。您需要爲此使用DescriptorEventListener,它可以像EntityListener一樣配置。

+0

我想通過不更新一個乾淨的(也就是說,沒有設置parentID並在@PreUpdate上設置ch.setParent(this))來管理它。如果您不會取消引用並再次引用您的子實體,那麼@PreUpdate似乎正在工作。不過謝謝! – 2012-04-12 01:38:25