Hello I am new to JPA Spring boot and right now I am trying to make a connection between two tables into a third one. So I have a Doctor and Patient table with it's properties, where one Doctor can examine every patient and a patient can visit every doctor.But in one examination there can be no more than one patient and one doctor. For the doctors I want to keep the information of which patients they have examined, respectively for the patients, which doctors they were examined from. I would like to create a middle table called DoctorVisit where I have the id of the doctor who did the examination and the id of the patient with some more properties like date,medicines and etc. When I try to do this I am getting an error - "mappedBy reference an unknown target entity property: /.../Patient.examinedByDoctors". If I remove the @OneToMany connection in Patient the code compiles. I would be really happy if someone can explain me where is the mistake. Thank you in advance
BaseEntity class:
@Getter
@Setter
@NoArgsConstructor
@MappedSuperclass
public class BaseEntity {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private long id;
}
Doctor class:
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name="doctor")
public class Doctor extends BaseEntity{
private String name;
@ManyToMany(mappedBy ="doctors")
private Set<Specialty> specialties;
@OneToMany(mappedBy ="doctor")
private Set<Patient> GpOfPatients;
@OneToMany(mappedBy = "doctor")
private List<Patient> examinedPatients;
}
Patient class:
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name="patient")
public class Patient extends BaseEntity{
private String name;
private String EGN;
private boolean insurancesPaidInLastSixMonths;
@ManyToOne
@JoinColumn(name="gp_id")
private Doctor doctor;
@OneToMany(mappedBy = "patient")
private List<Doctor> examinedByDoctors;
}
Specialty class:
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name="specialty")
public class Specialty extends BaseEntity{
private String specialtyName;
@ManyToMany
@JoinTable(name="doctors_specialties",joinColumns = @JoinColumn(name="specialty_id"),
inverseJoinColumns = @JoinColumn(name="doctor_id"))
private Set<Doctor> doctors;
}
DoctorVisit class:
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name="doctorvisit")
public class DoctorVisit extends BaseEntity {
@ManyToOne
@JoinColumn(name = "patient_id")
private Patient patient;
@ManyToOne
@JoinColumn(name="doctor_id")
private Doctor doctor;
private Date date;
private String diagonosis;
@ManyToMany(mappedBy = "prescribedToPatients")
private Set<Medicine> medicines;
private int patientChart;
}
Medicine class:
@Getter
@Setter
@Entity
@Table(name = "medicine")
public class Medicine extends BaseEntity{
private String name;
private String manufacturer;
@ManyToMany
@JoinTable(name="prescribedMedicines_to_patients",joinColumns = @JoinColumn(name="medicine_id"),
inverseJoinColumns = @JoinColumn(name="patient_id"))
private List<Patient> prescribedToPatients;
}
CodePudding user response:
You get this error because the Doctor class does not have a patient field. However adding a Patient won't fit your use case, because a Doctor can have multiple Patients not just one. So you have to create a ManyToMany association between Patient and Doctor or use only the DoctorVisit to connect the two entities. I would apply the second option and use special queries to get who visited who with the DISTINCT keyword for example.