Home > OS >  BeanCreationException on creating @manytomany entity relationship
BeanCreationException on creating @manytomany entity relationship

Time:12-08

I am creating three tables Doctor,Patient and Specialty. Patients can have exactly one GP, but one doctor can be a GP of many patients. Each Doctor has it's specialty(e.g. neurosurgeon,cardiologist,urologist etc.) When I try to make the many-to-many connection between Doctor and Specialty with a middle table I get an error "Error creating bean with name 'entityManagerFactory' defined in class path resource" caused by BeanCreationException in particular mappedBy reference an unknown target entity property. Since I am new to spring boot I would be really happy if someone can explain me. Thank you in advance

BaseEntity class:

@MappedSuperclass
public class BaseEntity {

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

Doctor class:

@Entity
@Table(name="doctor")
public class Doctor extends BaseEntity{

    private String name;

    @ManyToMany(mappedBy ="doctor")
    private Set<Specialty> specialties;

    @OneToMany(mappedBy ="doctor")
    private List<Patient> GpOfPatients;

}

Patient class:

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

}

Specialty class:

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

}

CodePudding user response:

In Doctor class, you mapped specialties with doctor, but in Specialty, the name of Doctor set is doctors, so they cannot be mapped.

You should change your Doctor class as shown below:

@Entity
@Table(name="doctor")
public class Doctor extends BaseEntity{

    private String name;

    @ManyToMany(mappedBy ="doctors")
    private Set<Specialty> specialties;

    @OneToMany(mappedBy ="doctor")
    private List<Patient> GpOfPatients;

}
  • Related