2011-04-29 45 views
1

如果我有这样的代码中的双向test_data.yml

@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 

我会得到这个错误:

Cannot load fixture initial-data.yml: No previous reference found for object of type children with key child1

但如果我不键入此:children: [child1, child2]然后我会有错误的结构,child1child2不会引用根。

+0

我固定:类别(child1): 标题:child1 类别(的child2): 标题:的child2 类别(目录root1): 标题:根 小孩:[child1,的child2] – ses 2011-04-29 09:06:59

回答

0

我做了一个类似的模型类似的事情:

Category(cat5): 
    title: Cameras & Photo 
Category(cat5.1): {title: Digital Camera, parent: cat5} 

它的工作原理,只要我使用YAML数据仅供引导。因为只设置父引用并且父类别的子列表保持未初始化状态。在Hibernate中,这是可行的,因为外键的设置是正确的。它不漂亮,但它适用于我。我还没有弄清楚如何处理yaml中的前向声明。