【问题标题】:Spring MVC can not delete a parent row?Spring MVC 不能删除父行?
【发布时间】:2019-06-28 11:52:37
【问题描述】:

我开始学习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 名学生,因为我无法删除一所学校。为此,我首先需要在此示例中删除孩子,孩子是删除父母(学校)后的学生。 但我认为我的情况不适用于此。请帮助我该怎么办?

【问题讨论】:

  • “但我认为我的方案不适用于此”。这是什么意思?
  • 是的,我的意思是我无法联系到 SchoolDAOImpl 班级的学生。这就是为什么当我尝试删除一所学校时,我无法联系到孩子(学生)。
  • @franiis 我解决了我的问题,我的朋友感谢您的帮助

标签: mysql spring-mvc jpa


【解决方案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,不是吗?

【讨论】:

  • 抱歉,我不明白在哪里可以在 SchoolDAOIMPl 类中添加您的意思的代码并在我的删除函数中创建新查询?
  • 感谢我的朋友,它有效。我一直在尝试这个 3 小时。我很感激你帮助我。但是学生没有从我的学生列表中删除。我想知道如何删除删除学校的学生。我尝试更新以更改删除,但没有成功。
  • 顺便说一下,我将在我的项目中添加注释作为我的进度。感谢您的帮助:)
  • 要删除而不是更新,请将更新语句更改为删除语句。 @内德
【解决方案2】:

您必须删除正在使用的任何数据库的完整性约束,或者您应该先删除子级然后删除父级,或者您可以在 hibernate https://thoughts-on-java.org/hibernate-tips-how-to-delete-child-entities/ 中查看其级联规则

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-02
    • 2018-08-28
    • 1970-01-01
    • 2018-03-29
    • 2016-12-16
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多