首页 > 代码库 > hibernate中“deleted object would be re-saved by cascade”异常原理和解决方案

hibernate中“deleted object would be re-saved by cascade”异常原理和解决方案

    在hibernate中,经常会出现one to many的关系,如果在不删除one端的前提下要删除many端的某一条或某几条记录,就可能出现上述的异常。

    该异常的原理是因为one 端和 many端的联系没有切断,必须要切断他们之间的联系才能进行删除。

    方法:

    Student s =  studentDao.findById(1);

    School.getStudents().remove(s);

    s.setSchool(null);

    stuedentDao.delete(s);

hibernate中“deleted object would be re-saved by cascade”异常原理和解决方案