Create java entities for join table

Viewed 35

I have a four-level hierarchy of java entities. Each level consists of an id and a list of entries of the next level.

E.g. (simplified to make the structure clear)

public class Top {
   private Long id;

   @OneToMany
   List<Upper> uppers;
}

public class Upper {
   private Long id;

   @ManyToOne
   Top parent;

   @OneToMany
   List<Lower> lowers;
}

public class Lower {
   private Long id;

   @ManyToOne
   Upper parent;

   @OneToMany
   List<Bottom> bottoms;
}

public class Bottom {
   private Long id;

   @ManyToOne
   Lower parent;
}

All data is stored in a single table/view with complete sets of top_id, upper_id, lower_id, bottom_id. So an entry's PK is the complete set of all four ids.

So rows would look like

top_id, upper_id, lower_id, bottom_id
1,      2,        3,        4
1,      5,        6,        7
8,      9,        10,       11
...

I tried to uses @Embedded or @EmbeddedId, had a look into @MapsId and some other annotations listed here: https://dzone.com/articles/all-jpa-annotations-mapping-annotations Most of the time I fail at the point where I need to specify the Id (composite or not) of Top. I am out of ideas on how to solve the problem. After all, I do not really need this model. I would be happy with a Map<Integer, Map<Integer, Map<Integer, List<Integer>>>>. However, I need to get this data without postprocessing in my spring boot application.

Any idea is appreciated.

0 Answers
Related