javaspring-bootrestspring-data-jpamodel

Trying to figure out how to join/link 3 different spring boot entities


I'm not even quite sure how to quickly word my question for a title, but what I'm attempting to do is: I have a tank entity that contains a list of RefLines. There is also a list of tests which each contain parameter inside. What I'm trying to do is link the refLine to that parameter based on the foreign keys the refLine has (one to parameter and one to tank).

Example entry

line_id: 1 (pk)
param_id: 1 (fk)
tank_id: 1 (fk)
value: 10
color: red

I was originally thinking of placing a one to many in the parameter entity but I don't know how to link the tank_id from that to the tank itself so only the refLines for that tank exist within that list. I feel like I'm not making much sense but hopefully enough to get some help. I can add the controllers/services/repositories if needed but I'm not sure if it would help as I'm not having issues with the code yet, just trying to see if this is possible or if I have to do something weird to make it work.


Solution

  • IIUC the relations between your entities are:
    1 tank to many reflines, 1 refline to 1 param.
    in that case the refline entity should be

    public class RefLine {
    
        @Id
        private int lineId;
    
        @OneToOne
        @JoinColumn(name = "param_id", referencedColumnName = "id")
        private Param param;
    
        @ManyToOne
        @JoinColumn(name = "tankId", insertable = false, updatable = false)
        private Tank tank;
        ...