Yaml用于自引用表

时间:2012-02-17 15:54:34

标签: playframework yaml one-to-many many-to-one

我的模型定义如下:

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 个答案:

答案 0 :(得分:1)

您的Link-to-Link父子关系的所有权在子对象上。所有权由

中的mappedBy-parameter定义
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)

因此,您应首先在YAML中声明父对象,并从父对象中省略“children”字段。然后,您可以在父对象声明之后添加子对象声明并添加字段

parent: focus

到子对象。它应该是这样的:

Link(focus):
    ...

Link(m):
    parent: focus

Link(hh):
    parent: focus