Cannot delete or update a parent row: a foreign key constraint fails

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sxf359/article/details/89086441

在执行hibernate对mysql数据库级联删除操作删除主表时出现此提示。

 org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [spring] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
 java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`springdemo`.`blog`, CONSTRAINT `fk_userid` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`))

经过反复尝试,最终发现是因为在mysql中设置外键的时候,把外键的删除时操作设置为RESTRICT造成。设置为cascade 级联操作,则问题解决。代码无需更改

展开阅读全文

没有更多推荐了,返回首页