我有两个连接的类,
病人:
...
@Entity
@Table(schema="public",name="patient")
public class Patient{
..
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "patient_id")
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "patient_examination", catalog = "ALTAdb", joinColumns = {
@JoinColumn(name = "patient_id", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "examination_id",nullable = false, updatable = false) })
public Set<Examination> getExaminations() {
return examinations;
}
public void setExaminations(Set<Examination> examinations) {
this.examinations = examinations;
}
...
检查:
...
@Entity
@Table(schema="public",name="examination")
public class Examination{
..
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "examination_id")
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "examinations")
public Set<Patient> getPatients() {
return patients;
}
public void setPatients(Set<Patient> patients) {
this.patients = patients;
}
...
当我尝试删除在patient_examination中出现的记录时
代码:
public boolean deleteExamination(int ExaminationId){
boolean deleted=false;
Session session=factory.getCurrentSession();
session.beginTransaction();
Query query =session.createQuery("select m from "+ Examination.class.getName() +" m");
List<Examination> examinations= query.getResultList();
for(Examination e: examinations){
if(e.getId()==ExaminationId){
session.delete(e);
deleted=true;
break;
}
}
session.getTransaction().commit();
return deleted;
}
这是我的控制台:
"Examination" violates the external key constraint "fkexas9b9fwxn0t37wl60a4jcd3" on the table "patient_examination"
Detail: The key (examination_id) = (4) is still referenced by the table "patient_examination"
我不知道如何解决它。
我也尝试过直接访问patient_examination的查询,但我不知道它的工作方法
答案 0 :(得分:0)
使用CascadeType.REMOVE尝试@ManyToMany,据我所知,它会起作用...