【问题标题】:JPA mappedBy reference an unknown target entity propertyJPA mappedBy 引用了一个未知的目标实体属性
【发布时间】:2022-01-13 12:05:22
【问题描述】:

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

BaseEntity 类:

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

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

医生类:

@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;
}

患者类别:

@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;

}

专业课:

@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 类:

@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;

}

医学类:

@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;

}

【问题讨论】:

    标签: java spring-boot entity-relationship


    【解决方案1】:

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

    【讨论】:

    • 嗯,这是我使用 DoctorVisit 表连接它们的主要想法,但仍然无法弄清楚如何避免我遇到的错误。 Doctor 类中的 List 检查Patients 是否足以使 DoctorVisit 表的一对多连接工作?
    • 啊,好吧,现在我想我得到了你想要达到的目标。起初我以为除了通过 DoctorVisit 之外,您还想直接将 Patient 与 Doctor 联系起来。您必须将 Patient.examinedByDoctors 的类型表单 List 更改为 List,因为此时您引用了错误的实体。并且还在 Doctor 中将 checkedPatients 更改为 List 类型。通过这些更改,您将通过 DoctorVisit 在患者和医生之间建立关联。
    • 哇,谢谢你,伙计,你让我的痛苦消失了。我以前从未使用过 Spring Boot,很难理解发生了什么。我真的很感谢你的帮助我很感激。
    • 很高兴能帮上忙 :) 我建议阅读这篇关于映射的文章:vladmihalcea.com/…(以及 Vlad 的任何其他博文)。
    • 一定会检查的,再次感谢你的朋友。
    猜你喜欢
    • 2021-05-02
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多