[英]JPA mappedBy reference an unknown target entity property
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.您好,我是 JPA Spring 引导的新手,现在我正在尝试将两个表之间的连接连接到第三个表。 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".我想创建一个名为 DoctorVisit 的中间表,其中我有进行检查的医生的 ID 和具有更多属性(如日期、药物等)的患者的 ID。当我尝试这样做时出现错误- “mappedBy 引用了一个未知的目标实体属性:/.../Patient.examinedByDoctors”。 If I remove the @OneToMany connection in Patient the code compiles.如果我删除 Patient 中的 @OneToMany 连接,代码就会编译。 I would be really happy if someone can explain me where is the mistake.如果有人可以向我解释错误在哪里,我会很高兴。 Thank you in advance先感谢您
BaseEntity class: BaseEntity class:
@Getter
@Setter
@NoArgsConstructor
@MappedSuperclass
public class BaseEntity {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private long id;
}
Doctor class: 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:患者 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:专业 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:医生访问 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:医药 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;
}
You get this error because the Doctor class does not have a patient field.您收到此错误是因为 Doctor class 没有患者字段。 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.所以你必须在 Patient 和 Doctor 之间创建一个 ManyToMany 关联,或者只使用 DoctorVisit 来连接这两个实体。 I would apply the second option and use special queries to get who visited who with the DISTINCT keyword for example.例如,我将应用第二个选项并使用特殊查询来获取谁使用DISTINCT关键字访问了谁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.