我一直在研究一个实体,oneToMany 关系,问题是每当我使用它时:
//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 时,尤其是在干净更新实体时,Parent 的 Id 没有与子实体(作为外键)一起保存。请帮忙...
似乎@PreUpdate 不起作用,@PrePersist 完全起作用。