B.obed
B.obed

Reputation: 119

JPA , Spring Mapping multiple entities

I have 3 tables Consultant, Patient and Diagnosis. Diagnosis has attributes of both Consultant and Patient as foreign keys. I want to know how to show this in spring. What i have so far is, Consultant.java

   private String name;
   private String pos;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "consultant")
    private List<Diagnosis> diagnosis;

Diagnosis.java

public class Request {
    private String token;
    private String comment;
    private boolean status;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "Patient_id")
    @JoinColumn(name = "Consultant_id") //not sure about this syntax
    private Consultant consultant;
    private Patient patient;

Patient.java

 private String name;
   private int pid;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "patient")
    private List<Diagnosis> diagnosis;

Please is this how to go about it? I'm using postgresql.

Upvotes: 0

Views: 264

Answers (1)

Andronicus
Andronicus

Reputation: 26076

Why are you annotating one field if you have 2 entities? Every relationship should be properly annotated:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Patient_id")
private Patient patient;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Consultant_id")
private Consultant consultant;

Upvotes: 2

Related Questions