问题:
我在父母和孩子之间有一个@OneToOne关系,该关系由孩子映射,并且 orphanRemoval设置为true。孩子还与第三个实体有@ManyToMany 关系。
将孩子设置为空(希望删除孩子)时,我得到一个完整性约束违规:外键...
测试型号:
Tutor与Student具有OneToOne 关系, Student 与Course实体具有 ManyToMany 关系:
- OneToOne 关系由 Student.tutor 映射(STUDENT 表中的 ID_TUTOR 外键列)
- 级联 = 全部
- 孤儿移除 = 真
- ManyToMany 关系保存在连接表 STUDENT_COURSE 中,外键指向 STUDENT 和 COURSE 表。
我正在使用休眠 5.0.11 (JPA 2.1)。对于我的测试,我使用了 HSQLDB,但问题也出现在 Oracle 数据库上。
设想:
我有一个导师链接到一个包含课程列表的学生。当设置Tutor的Student为null时,Hibernate直接发出SQL“ delete from Student where id=? ”导致“完整性约束违规:外键无动作;FK1XM2HEI9CHMWOQF2WFM104NMG表:STUDENT_COURSE”
当 OneToMany 关系未“反转”(TUTOR 表中没有mappedBy和 ID_STUDENT 外键)时,不会发生此问题。在这种情况下,Student 实体以及 STUDENT_COURSE 表中的相关记录已正确地从 DB 中删除。
代码:
@Entity
public class Tutor {
private Long id;
private String name;
private Student student;
public Tutor() { super(); }
public Tutor(String name, Student student) {
super();
this.name = name;
setStudent(student);
}
@Id
@GeneratedValue
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@OneToOne(cascade=CascadeType.ALL, orphanRemoval=true, mappedBy="tutor")
public Student getStudent() { return student; }
public void setStudent(Student student) {
this.student = student;
if(student != null) {
student.setTutor(this);
}
}
}
@Entity
public class Student {
private Long id;
private String name;
private Map<String, Course> courses;
private Tutor tutor;
public Student() { super(); }
public Student(String name, Map<String, Course> courses) {
super();
this.name = name;
this.courses = courses;
}
@Id
@GeneratedValue
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@ManyToMany
public Map<String, Course> getCourses() { return courses; }
public void setCourses(Map<String, Course> courses) { this.courses = courses; }
@OneToOne
public Tutor getTutor() { return tutor; }
public void setTutor(Tutor tutor) { this.tutor = tutor; }
}
@Entity
public class Course {
private Long id;
private String name;
public Course() { super(); }
public Course(String name) {
super();
this.name = name;
}
@Id
@GeneratedValue
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
public class CheckManyToManyDeletation {
private static final Logger logger = Logger.getLogger(CheckManyToManyDeletation.class);
private static SessionFactory sessionFactory;
public static void main(String[] args) {
DOMConfigurator.configure("log4j-config.xml");
Class[] mappings = new Class[] {Tutor.class, Student.class, Course.class};
DBUtils.createTables(mappings);
sessionFactory = DBUtils.createSessionFactory(mappings);
try {
long tutorId = initialImport();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Tutor tutor = session.get(Tutor.class, tutorId);
logger.info("DeletingStudent");
tutor.setStudent(null);
tx.commit();
} finally {
sessionFactory.close();
}
}
private static long initialImport() {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Student student = new Student();
student.setName("student");
Map<String, Course> courses = new HashMap<>();
student.setCourses(courses);
for (int i = 0; i < 5; i++) {
Course course = new Course();
course.setName("course" + i);
session.save(course);
courses.put(course.getName(), course);
}
Tutor tutor = new Tutor("tutor", student);
session.save(tutor);
tx.commit();
session.close();
return tutor.getId();
}
}
笔记:
实际上,我们的应用程序带有一个巨大的数据模型:
- 持久化和删除实体是通过级联完成的,
- 我们反转 OneToOne 关系以赋予子代与其父代相同的 id(通过 @MapsId)。