我正在努力做出这么简单的事情:我想要一个树关系(忘记性能,它会变浅,不超过2-3级)。我试过这个:
@Entity @Table(name =“BASE”) 公共类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")
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;
}
}
我通过即时两个豆A和B来测试它。保存A.将A设置为B的父级。保存B.然后从db A加载我期待该集合包含B,但没有运气。儿童财产的大小()= 0.我甚至没有尝试删除..但我很悲观。
帮助?