mysql - Spring MVC 不能删除父行?
问题描述
我开始学习spring mvc。我正在做一些练习,我遇到了一个逻辑错误。在我的示例中,我在 spring mvc 中有 2 个实体类。在那里我的代码 school_ıd 是主键。当我尝试从学校列表中删除一所学校时,我给出了这样的错误
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails
实体学校:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="school_id")
private int schoolId;
@Column(name="school_name")
private String schoolName;
public int getSchoolId() {
return schoolId;
}
public void setSchoolId(int schoolId) {
this.schoolId = schoolId;
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
实体学生
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="email")
private String email;
@OneToOne
@JoinColumn(name="school_id")
private School school;
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
学校控制器删除方法:
@GetMapping("/deleteSchool")
public String deleteSchool(@RequestParam("schoolID") int
theSchoolId) {
schoolService.deleteSchool(theSchoolId);
return "redirect:/school/list";
}
删除方法 SchoolDAOImpl:
@Override
public void deleteSchool(int theSchoolId) {
Session currentSession=sessionFactory.getCurrentSession();
Query theQuery=currentSession.createQuery("delete from School where id=:schoolID");
theQuery.setParameter("schoolID", theSchoolId);
theQuery.executeUpdate();
}
实际上我现在的问题是我试图删除一所学校,theSchool 至少有 1 名学生,因为我无法删除一所学校。为此,我首先需要在此示例中删除孩子,孩子是删除父母(学校)后的学生。但我认为我的情况不适用于此。请帮我我该怎么办?
解决方案
您可以先更新学生:
@Override
public void deleteSchool(int theSchoolId) {
Session currentSession=sessionFactory.getCurrentSession();
Query updateStudentQuery=currentSession.createQuery("Update Student s SET s.school = null WHERE s.school.schoolId = :schoolId");
updateStudentQuery.setParameter("schoolID", theSchoolId);
updateStudentQuery.executeUpdate();
Query theQuery=currentSession.createQuery("delete from School where id=:schoolID");
theQuery.setParameter("schoolID", theSchoolId);
theQuery.executeUpdate();
}
或使用学生的 DAO 更新,然后执行学校的删除查询。
我无法测试,所以我不知道它是否适用于“WHERE s.school.schoolId = :schoolId”,或者您必须使用子查询创建 where。如果不起作用,请尝试使用本机查询:
Query updateStudentQuery=currentSession.createNativeQuery("sql here");
代替
Query updateStudentQuery=currentSession.createQuery("Update Student s SET s.school = null WHERE s.school.schoolId = :schoolId");
此外,如果您想建立双向关系,请将其添加到 School Entity:
@OneToOne(mappedBy = "school")
private Student student;
顺便说一句,我猜这个关系是@ManyToOne 而不是@OneToOne,不是吗?
推荐阅读
- node.js - Typescript - 创建自定义快速中间件定义
- google-apps-script - Google Sheets 间歇性 IMPORTRANGE 不起作用,应用脚本无法从单元格中读取数据
- python - 如何从其他文件导入 Python 代码
- javascript - 不记录或检索数据
- laravel - 如何在 Eloquent 上获得查询响应时间?
- matlab - 将单应性应用于一组点并在变形图像上显示这些点
- java - 如何只保留整数的最后 3 位并能够操作它们?
- audio - 如何在文件副本之间同步音频元数据(例如 ID3)?
- android - 从具有相同包名的应用程序中读取文件
- python - 将 bqplot 工具提示与数据框一起使用