Open Jpa OneToMany parent relationship with two level down child

36 Views Asked by At

How can I make a relationship with a second-level child using openjpa.

Parent -> Child -> GrandChild

Parent has a property lets say, plant, the child does not have it, however the grand child do. So, how can I make a relationship to the grand child directly from parent in order to get only those children having same plant as their parent in their child level(GrandChild). (Preferably one sided).

1

There are 1 best solutions below

0
On

The simplest option is to move from Parent --> Plant --> GrandChilds.

Meaning you will need:

  • A ManyToOne in the Parent with Plant
  • A OneToMany in the Plant with List< GrandChild >

Additionally (to be coherent)

  • A OneToMany in the Plant with List< Parent >
  • A ManyToOne in the GrandChild with Plan

After doing so you will be able to do:

parent.getPlant().getGrandChilds()