Question

I have a model defined as follows:

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;
}

I am unable to specify parent for the first two items in the yaml to compete my data

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
Was it helpful?

Solution

The ownership of your Link-to-Link parent-child relationship is on the child object. The ownership is defined by mappedBy-parameter in

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

Therefore you should first declare your parent objcect in YAML, and omit the "children" field from the parent. Then you can add the children object declarations after the parent object declaration and add fields

parent: focus

to the children objects. It should be something like this:

Link(focus):
    ...

Link(m):
    parent: focus

Link(hh):
    parent: focus
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top