JPA mappedBy 引用未知目标实体 属性

JPA mappedBy reference an unknown target entity property

您好,我是 JPA Spring 引导新手,现在我正在尝试将两个 table 连接到第三个。所以我有一个 Doctor and Patient table 具有它的属性,其中一名医生可以检查每位患者,一名患者可以访问每个 doctor.But 在一次检查中不能超过一名患者和一名医生。对于医生,我想保留他们检查过哪些患者的信息,分别针对患者,他们接受过哪些医生的检查。我想创建一个名为 DoctorVisit 的中间 table ,其中我有进行检查的医生的 ID 和具有更多属性(如日期、药物等)的患者的 ID。当我尝试这样做时,我我收到一个错误 - “mappedBy 引用了一个未知的目标实体 属性: /.../Patient.examinedByDoctors”。如果我删除 Patient 中的 @OneToMany 连接,代码就会编译。如果有人能向我解释错误在哪里,我会很高兴。提前谢谢你

基础实体class:

@Getter
@Setter
@NoArgsConstructor
@MappedSuperclass
public class BaseEntity {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private long id;
}

医生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;
}

患者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;

}

专业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;

}

看医生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;

}

医学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;

}

您收到此错误是因为 Doctor class 没有 patient 字段。但是,添加 Patient 不适合您的用例,因为 Doctor 可以有多个 Patient 而不仅仅是一个。因此,您必须在 Patient 和 Doctor 之间创建 ManyToMany 关联,或者仅使用 DoctorVisit 来连接两个实体。我会应用第二个选项并使用特殊查询来获取谁访问了谁,例如使用 DISTINCT 关键字。