My App在MySQL中有两个表:Hospital和Doctor,与JoinTable hospital_doctor联系@ManyToMany。
我为医院和医生写了简单的CRUD操作来获取数据,但是有一个问题......
我想获得在医院的医生名单。
所以我写了这个简单的操作:
我的回购:
@Repository
public interface DoctorDao extends JpaRepository<Doctor, Integer>{
public List<Doctor> findByHospitalsId(int hospitalsId);
}
服务:
public List<Doctor> getDoctorsByHospital(int hospitalsId){
List<Doctor> doctors = new ArrayList<>();
doctorDao.findByHospitalsId(hospitalsId).forEach(doctors::add);
return doctors;
}
和休息控制器:
@GetMapping(value = "api/hospitals/{hospitalsId}/doctors")
public List<Doctor> getDoctorsByHospitalId(@PathVariable int hospitalsId){
return doctorService.getDoctorsByHospital(hospitalsId);
}
我可以在邮递员中获取“/ api / hospitals / 2”的数据(我在MySql工作台中插入此数据):
[
{
"name": "Santa barbara",
"country": "USA",
"town": "Philadelphia",
"street": "Saint John",
"postal_code": "12-124",
"phone_number": "123-123",
"fax_number": "1231451",
"number_of_ambulances": 1,
"helicopter_access": false,
"teaching_hospital": false,
"doctors": [
{
"id": 1,
"name": "John",
"surname": "Doe",
"title": "doctor",
"license_number": 2453,
"phone": "234234",
"email": "asdf!@sdf.sdf",
"nationality": "PL",
"speciality": "asdf",
"date_of_birth": 1355266800000,
"is_a_teacher": true
}
],
"id": 2
}
现在,当我得到/ api / hospital / 2 /医生时,我想制作应用程序它会返回所有医生的数据(在这种情况下是一个John博士)。
但是当我向/ api / hospital / 2 /医生发送GET请求时,它会返回500服务器错误:
无法提取ResultSet; SQL [不适用];嵌套异常是org.hibernate.exception.SQLGrammarException:无法提取ResultSet“,
并且它是 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:我的控制台中的表'health.doctor_hospitals'不存在。
我的模特:
医生模型
@Entity
public class Doctor {
private Set<Hospital> hospitals;
@Column(name = "Id")
int id;
@Column(name = "name")
String name;
@Column(name = "surname")
String surname;
@Column(name = "title")
String title;
@Column(name = "license_number", unique = true)
int license_number;
@Column(name = "phone")
String phone;
@Column(name = "email")
String email;
@Column(name = "nationality")
String nationality;
@Column(name = "speciality")
String speciality;
@Temporal(TemporalType.DATE)
@Column(name = "date_of_birth")
Date date_of_birth;
@Column(name = "is_a_teacher")
boolean is_a_teacher;
public Doctor() {
}
public Doctor(Set<Hospital> hospitals, String name, String surname, String
title, int license_number, String phone,
String email, String nationality, String speciality, Date
date_of_birth, boolean is_a_teacher) {
super();
this.hospitals = hospitals;
this.name = name;
this.surname = surname;
this.title = title;
this.license_number = license_number;
this.phone = phone;
this.email = email;
this.nationality = nationality;
this.speciality = speciality;
this.date_of_birth = date_of_birth;
this.is_a_teacher = is_a_teacher;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getLicense_number() {
return license_number;
}
public void setLicense_number(int license_number) {
this.license_number = license_number;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
public String getSpeciality() {
return speciality;
}
public void setSpeciality(String speciality) {
this.speciality = speciality;
}
public Date getDate_of_birth() {
return date_of_birth;
}
public void setDate_of_birth(Date date_of_birth) {
this.date_of_birth = date_of_birth;
}
public boolean isIs_a_teacher() {
return is_a_teacher;
}
public void setIs_a_teacher(boolean is_a_teacher) {
this.is_a_teacher = is_a_teacher;
}
@JsonBackReference
@ManyToMany
public Set<Hospital> getHospitals() {
return hospitals;
}
public void setHospitals(Set<Hospital> hospitals) {
this.hospitals = hospitals;
}
}
医院模型 @Entity
public class Hospital {
@Column(name = "Id")
int Id;
@Column(name = "name", unique = true)
String name;
@Column
String country;
@Column
String town;
@Column
String street;
@Column
String postal_code;
@Column
String phone_number;
@Column
String fax_number;
@Column
int number_of_ambulances;
@Column
boolean helicopter_access;
@Column
boolean teaching_hospital;
public Set<Doctor> doctors;
public Hospital() {
}
public Hospital(String name, String country, String town, String street,
String postal_code, String phone_number,
String fax_number, int number_of_ambulances, boolean
helicopter_access, boolean teaching_hospital,
Set<Doctor> doctors) {
super();
this.name = name;
this.country = country;
this.town = town;
this.street = street;
this.postal_code = postal_code;
this.phone_number = phone_number;
this.fax_number = fax_number;
this.number_of_ambulances = number_of_ambulances;
this.helicopter_access = helicopter_access;
this.teaching_hospital = teaching_hospital;
this.doctors = doctors;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getPostal_code() {
return postal_code;
}
public void setPostal_code(String postal_code) {
this.postal_code = postal_code;
}
public String getPhone_number() {
return phone_number;
}
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}
public String getFax_number() {
return fax_number;
}
public void setFax_number(String fax_number) {
this.fax_number = fax_number;
}
public int getNumber_of_ambulances() {
return number_of_ambulances;
}
public void setNumber_of_ambulances(int number_of_ambulances) {
this.number_of_ambulances = number_of_ambulances;
}
public boolean isHelicopter_access() {
return helicopter_access;
}
public void setHelicopter_access(boolean helicopter_access) {
this.helicopter_access = helicopter_access;
}
public boolean isTeaching_hospital() {
return teaching_hospital;
}
public void setTeaching_hospital(boolean teaching_hospital) {
this.teaching_hospital = teaching_hospital;
}
@JsonManagedReference
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "hospital_doctor", joinColumns = @JoinColumn(name = "hospital_id", referencedColumnName = "Id"), inverseJoinColumns = @JoinColumn(name = "doctor_id", referencedColumnName = "Id"))
public Set<Doctor> getDoctors(){
return doctors;
}
public void setDoctors(Set<Doctor> doctors) {
this.doctors = doctors;
}
}
医院医生加入
@Entity
@Table(name="hospital_doctor")
public class HospitalDoctor implements Serializable{
private static final long serialVersionUID = 1L;
private Doctor doctor;
private Hospital hospital;
private Date contract_start_date;
private Date contract_end_date;
private String position;
private String supervisor;
private boolean part_time;
@Id
@ManyToOne
@JoinColumn(name="doctor_id")
public Doctor getDoctor() {
return doctor;
}
public void setDoctor(Doctor doctor) {
this.doctor = doctor;
}
@Id
@ManyToOne
@JoinColumn(name="hospital_id")
public Hospital getHospital() {
return hospital;
}
public void setHospital(Hospital hospital) {
this.hospital = hospital;
}
public Date getContract_start_date() {
return contract_start_date;
}
public void setContract_start_date(Date contract_start_date) {
this.contract_start_date = contract_start_date;
}
public Date getContract_end_date() {
return contract_end_date;
}
public void setContract_end_date(Date contract_end_date) {
this.contract_end_date = contract_end_date;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getSupervisor() {
return supervisor;
}
public void setSupervisor(String supervisor) {
this.supervisor = supervisor;
}
public boolean isPart_time() {
return part_time;
}
public void setPart_time(boolean part_time) {
this.part_time = part_time;
}
}