SSM捕获异常DataIntegrityViolationException和MySQLIntegrityConstraintViolationException

一、错误提示

ssm项目删除时出现错误如下:
SSM捕获异常DataIntegrityViolationException和MySQLIntegrityConstraintViolationException_第1张图片

org.springframework.web.util.NestedServletException: Request
processing failed; nested exception is
org.springframework.dao.DataIntegrityViolationException:
#Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Cannot delete or update a parent row: a foreign key constraint fails
(test.project_change, CONSTRAINT FK_Reference_35 FOREIGN KEY
(project_id) REFERENCES project (project_id))
#The error may exist in class1/group5/rispiu/dao/mybatis/ProjectMapper.java (best guess)
#The error may involve class1.group5.rispiu.dao.mybatis.ProjectMapper.deleteByPrimaryKey-Inline
#The error occurred while setting parameters
#SQL: delete from project where project_id = ?
#Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Cannot delete or update a parent row: a foreign key constraint fails
(test.project_change, CONSTRAINT FK_Reference_35 FOREIGN KEY
(project_id) REFERENCES project (project_id)) ; Cannot delete or
update a parent row: a foreign key constraint fails
(test.project_change, CONSTRAINT FK_Reference_35 FOREIGN KEY
(project_id) REFERENCES project (project_id)); nested exception
is
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Cannot delete or update a parent row: a foreign key constraint fails
(test.project_change, CONSTRAINT FK_Reference_35 FOREIGN KEY
(project_id) REFERENCES project (project_id))

二、出错源代码

ProjectServiceImp.java

    @Override
    public int deleteProject(Long projectId){
        System.out.println("项目标号是:" + projectId);
        return projectMapper.deleteByPrimaryKey(projectId);
    }
  ProjectController.java
    public String deleteProject(Long projectId) {
      projectService.deleteProject(projectId);
      return "redirect:/projectManagement";
  }

三、尝试解决

一开始只留意到以下错误,然后尝试捕获,结果提示该异常不能捕获com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
选择类SSM捕获异常DataIntegrityViolationException和MySQLIntegrityConstraintViolationException_第2张图片
然后看到很多博友给出都是改数据库的设置,对我来说无济于事,我只是想捕获异常,后来找到一个靠谱的方法如下。

四、解决方法

Mybatis不鼓励捕捉异常,但在某些特定场景下需要捕捉,这个时候通过try catch是捕捉不到的,mybatis有自己的处理方式,它把异常映射成了DataAccessException,那么我们需要抛出异常并捕捉。

修改后的代码:
ProjectServiceImp.java

    @Override
    public int deleteProject(Long projectId) throws DataAccessException{
        System.out.println("项目标号是:" + projectId);
        System.out.println("删除返回值:" + projectMapper.deleteByPrimaryKey(projectId));
        return projectMapper.deleteByPrimaryKey(projectId);
    }

ProjectController.java

   @RequestMapping("/deleteProject.do")
   public String deleteProject(Long projectId, HttpServletRequest request) {
      try {
         if (projectService.deleteProject(projectId) == 1) {
            request.setAttribute("info", "删除成功");
         }
      }catch (DataAccessException e){
         request.setAttribute("info", "删除失败");
         System.out.println("删除错误:"+e);
      }
      return "redirect:/projectManagement";
   }

你可能感兴趣的:(SSM+MAVEN的web开发)