我在父母和孩子之间有 @OneToOne 关系,关系是由孩子映射,orphanRemoval 设置为true。孩子与第三个实体也有@ManyToMany关系。
将子项设置为null(希望删除子项)我得到完整性约束违规:外键...
导师与学生具有OneToOne关系,与课程实体具有ManyToMany关系:
我使用的是Hibernate 5.0.11(JPA 2.1)。对于我的测试,我使用的是HSQLDB,但问题也发生在Oracle数据库上。
我有一个导师链接到学生,其中包含课程列表。当将Tutor的Student设置为null Hibernate时,直接从Student发出SQL" delete,其中id =?"导致"完整性约束违规:外键无动作; FK1XM2HEI9CHMWOQF2WFM104NMG表:STUDENT_COURSE"
当OneToMany关系不是"反转时,不会发生此问题" (TUTOR表中没有 mappedBy 和ID_STUDENT外键)。在这种情况下,学生实体将从数据库中正确删除,并在STUDENT_COURSE表中删除相关记录。
@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();
}
}
实际上我们的应用程序带有一个庞大的数据模型: