MySQLIntegrityConstraintViolationException :: column不能为null

时间:2018-02-21 18:55:07

标签: mysql hibernate rest jpa spring-boot

我的表中有双向OneToMany-manyToOne关系,我得到 MySQLIntegrityConstraintViolationException:Column' hospital_id'不能为空。

你可以帮帮我吗?我在这上花了3个小时......

这是我的代码:

@Entity
@Table(name="hospital_doctor")
@IdClass(HospitalDoctor.class)
public class HospitalDoctor implements Serializable{

private static final long serialVersionUID = 1L;


private Doctor doctor;
private Hospital hospital;

@Id
private int id;
@Temporal(TemporalType.DATE)
private Date contract_start_date;
@Temporal(TemporalType.DATE)
private Date contract_end_date;
private String position;
private String supervisor;
private boolean part_time;



public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}


@Id
@ManyToOne
@JsonManagedReference
@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;
} 

 /**GETTERS AND SETTERS

AND MODEL CLASSES:

@Entity

public class Hospital {

    private Set<HospitalDoctor> hospitalDoctors;


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





@JsonBackReference
@OneToMany(fetch= FetchType.LAZY, mappedBy="doctor", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<HospitalDoctor> getHospitalDoctors() {
    return hospitalDoctors;
}


public void setHospitalDoctors(Set<HospitalDoctor> hospitalDoctors) {
    this.hospitalDoctors = hospitalDoctors;
}


@Id
public int getId() {
    return Id;
}

public void setId(int id) {
    Id = id;
}
  //Rest of getters and setters

最后一个:

   @Entity
public class Doctor {

    private Set<HospitalDoctor> hospitalDoctors;




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

@JsonBackReference
@OneToMany(fetch= FetchType.LAZY, mappedBy="doctor", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<HospitalDoctor> getHospitalDoctors() {
    return hospitalDoctors;
}



public void setHospitalDoctors(Set<HospitalDoctor> hospitalDoctors) {
    this.hospitalDoctors = hospitalDoctors;
}

@Id
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

我的服务方法(使用JpaRepository)

    public void addHospitalDoctor(HospitalDoctor hospitalDoctor) {
    hospitalDoctorDao.save(hospitalDoctor);
}

和RestController

@PostMapping(value="/api/hospitaldoctors")
public void addHospitalDoctor(HospitalDoctor hospitalDoctor){
    hospitalDoctorService.addHospitalDoctor(hospitalDoctor);
}

如果anyOne可以帮助我,我会非常高兴。干杯:)

0 个答案:

没有答案