在hibernate中创建树关系

在hibernate中创建树关系,hibernate,Hibernate,我正在努力使这件事变得简单:我想要一个树关系(忘记性能,它将是肤浅的,不超过2-3级)。我试过这个: @实体 @表(name=“BASE”) 公共类BaseEBean{ int id; String text; BaseEBean parent; Set<BaseEBean> children = new HashSet<BaseEBean>(); @Id @GeneratedValue @Column(name = "ID") public int getId()

我正在努力使这件事变得简单:我想要一个树关系(忘记性能,它将是肤浅的,不超过2-3级)。我试过这个:

@实体 @表(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;
}       
int-id;
字符串文本;
作为父母;
Set children=newhashset();
@身份证
@生成值
@列(name=“ID”)
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
@列(name=“TEXT”)
公共字符串getText(){
返回文本;
}
公共void setText(字符串文本){
this.text=文本;
}
@许多酮
@级联(值={CascadeType.ALL})
public baseBean getParent(){
返回父母;
}
public void setParent(baseBean parent){
this.parent=parent;
}
@OneToMany(mappedBy=“parent”,fetch=FetchType.EAGER)
公共集getChildren(){
返回儿童;
}
公共无效集合子对象(集合子对象){
这个。孩子=孩子;
}       
}

我通过实例化两个bean A和B来测试它。保存A。将A设置为B的父项。正在保存B。然后从db A加载。我希望集合包含B,但运气不好。子属性的大小()将为0。我甚至没有尝试删除。。但我是悲观的


帮助?

是否检查了数据库以查看A和B是否被持久化?另外,请尝试仅拉出B并查看A是否被列为其父级,这将帮助我们更好地了解发生了什么。如果我在另一个会话中执行此操作,关闭现有会话,A和B都将正常。所以