I'm striving to make this simple thing: I want a tree relationship (forget about performance, it is gonna be shallow, no more than 2 -3 levels). I tried this:
@Entity @Table(name = "BASE") public class BaseEBean {
int id;
String text;
BaseEBean parent;
Set<BaseEBean> children = new HashSet<BaseEBean>();
@Id
@GeneratedValue
@Column(name = "ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "TEXT")
开发者_Python百科public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@ManyToOne
@Cascade(value = { CascadeType.ALL })
public BaseEBean getParent() {
return parent;
}
public void setParent(BaseEBean parent) {
this.parent = parent;
}
@OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
public Set<BaseEBean> getChildren() {
return children;
}
public void setChildren(Set<BaseEBean> children) {
this.children = children;
}
}
I tested it by instanting two beans A and B. Saving A. Setting A as B's parent. Saving B. Then loading from db A. I was expecting the set to contain B, but no luck. Children property will have size() = 0. I didn't even try to delete.. but I am pessimistic.
Help?
精彩评论