【发布时间】:2011-04-29 08:36:53
【问题描述】:
如果我有这个代码
@Entity
public class Category extends Model {
public String title;
public Category() {}
public Category(String title) {
this.title = title;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="parent")
public List<Category> children = new LinkedList<Category>();
@ManyToOne
@JoinColumn(name="parent", insertable=false, updatable=false)
public Category parent;
public void addChild(Category child) {
Category root = this;
child.parent = root;
root.children.add(child);
root.save();
}
}
我想在我的 test_data.yml 文件中创建测试数据,如何在其中输入?我的意思是这是双向的..
例如,如果我会这样做:
Category(root1):
title: root
children: [child1, child2]
Category(child1):
title: child1
parent: root1
Category(child2):
title: child2
parent: root1
我会收到这个错误:
无法加载夹具初始数据.yml: 未找到对象的先前参考 具有关键子项的子项1
但如果我不输入这个:children: [child1, child2],那么我的结构就会错误,child1 和 child2 不会引用到 root。
【问题讨论】:
-
我固定:类别(child1):标题:child1 类别(child2):标题:child2 类别(root1):标题:root 孩子:[child1,child2]
标签: hibernate jpa playframework