删除分类是不是可以优化?

删除分类是不是可以优化?

删除分类的时候,如果没有删除三级分类,直接删除二级分类是不是会报错?必须把三级分类删除完了,才能删除二级分类,可以优化成点击删除二级分类的时候就直接把下属的三级分类删除掉吗?

http://img1.sycdn.imooc.com//climg/600f494e090c99b109740209.jpg

java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at com.imooc.icake.global.GlobalController.service(GlobalController.java:60)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at com.imooc.icake.global.LoginFilter.doFilter(LoginFilter.java:20)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at com.imooc.icake.global.EncodingFilter.doFilter(EncodingFilter.java:31)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:615)

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1627)

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:748)

Caused by: org.apache.ibatis.exceptions.PersistenceException: 

### Error updating database.  Cause: java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`icake`.`catalog`, CONSTRAINT `FK_Reference_2` FOREIGN KEY (`pid`) REFERENCES `catalog` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT)

### The error may exist in com/imooc/icake/dao/CatalogDao.java (best guess)

### The error may involve com.imooc.icake.dao.CatalogDao.delete-Inline

### The error occurred while setting parameters

### SQL: delete from catalog where id=?

### Cause: java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`icake`.`catalog`, CONSTRAINT `FK_Reference_2` FOREIGN KEY (`pid`) REFERENCES `catalog` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT)

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)

at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:199)

at org.apache.ibatis.session.defaults.DefaultSqlSession.delete(DefaultSqlSession.java:212)

at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:72)

at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)

at com.sun.proxy.$Proxy18.delete(Unknown Source)

at com.imooc.icake.biz.impl.CatalogBizImpl.remove(CatalogBizImpl.java:27)

at com.imooc.icake.controller.CatalogController.remove(CatalogController.java:66)

... 33 more

Caused by: java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`icake`.`catalog`, CONSTRAINT `FK_Reference_2` FOREIGN KEY (`pid`) REFERENCES `catalog` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT)

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117)

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)

at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)

at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)

at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:370)

at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:47)

at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)

at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)

at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)

at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)

at com.sun.proxy.$Proxy17.update(Unknown Source)

at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)

... 39 more


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,如下报错提示是指删除或更新时由于外键造成的报错。

http://img1.sycdn.imooc.com//climg/600fb954096baac808060098.jpg

如果同学想删除一级分类的同时删除该分类下的二级及三级分类,需要在创建表时,需要设置级联操作操作,如:

http://img1.sycdn.imooc.com//climg/600fbd8709e0b51d06360261.jpg

这样当删除一级分类时,该分类下的所有分类都会被删除。例如:

删除分类id为14200的分类,该分类下的所有分类也被删除。

http://img1.sycdn.imooc.com//climg/600fbdda09f2598205370846.jpg

​祝学习愉快~

  • QQ71 #1

    老师你好,请问实际工作中更多是是在业务层实现的级联还是直接在数据库中设定好的级联呢?

    2021-01-28 11:38:38
  • 同学你好,实际工作中,更多的是在业务层实现级联删除。

    祝学习愉快~

    2021-01-28 14:19:46
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师