2012-04-11 47 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