2012-02-17 49 views
1

我已經定義爲模型如下:YAML自引用表

public class Link extends Model { 

    @Required 
    public String tag; 
    @Required 
    public String type; 
    @Required 
    public int weight; 

    @ManyToOne(cascade = CascadeType.ALL) 
    public Link parent; 

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) 
    public List<Link> children; 

    @ManyToMany(cascade = CascadeType.PERSIST) 
    public List<Tag> tags; 
} 

,我不能指定父在YAML前兩項競爭我的數據

Link(m): 
    tag: m 
    type: home 
    weight: 1 
    tags: 
     - tagH 
     - tagM 

Link(hh): 
    tag: hh 
    type: home 
    weight: 2 
    tags: 
     - tagH 
     - tagHH 

artoo.Link(focus): 
    tag: focus 
    type: footer 
    weight: 1 
    tags: 
      - tagTechnology 
      - tagLegal 
    children: 
      - m 
      - hh 

回答

1

的Link-to-Link父子關係的所有權在子對象上。所有權是通過的mappedBy參數在

@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) 

定義因此,你應該首先聲明你的父母objcect在YAML,並從父省略「孩子」字段。然後,您可以添加子對象的父對象聲明後,聲明和字段添加

parent: focus 

到子對象。它應該是這樣的:

Link(focus): 
    ... 

Link(m): 
    parent: focus 

Link(hh): 
    parent: focus