多表刪除報錯:
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
ou\javax\el\javax.el-api\2.2.4\javax.el-api-2.2.4.jar;E:\local_repository\repository_pinyougou\org\glassfish\web\javax.el\2.2.4\javax.el-2.2.4.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.wsc.core.onetone.ManyToMany,deleteUser
log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select user0_.user_id as user_id1_6_0_, user0_.password as password2_6_0_, user0_.username as username3_6_0_, role1_.userid as userid1_7_1_, role2_.role_id as roleid2_7_1_, role2_.role_id as role_id1_5_2_, role2_.memo as memo2_5_2_, role2_.role_name as role_nam3_5_2_ from user user0_ left outer join user_role role1_ on user0_.user_id=role1_.userid left outer join role role2_ on role1_.roleid=role2_.role_id where user0_.user_id=?
Hibernate: delete from user_role where userid=?
Hibernate: delete from role where role_id=?
Hibernate: delete from role where role_id=?
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`shop`.`user_role`, CONSTRAINT `FKbo5ik0bthje7hum554xb17ry6` FOREIGN KEY (`roleid`) REFERENCES `role` (`role_id`))
分析:想要刪除單個表數據報錯
我的報錯是在刪除時:主要將中間表的外鍵約束刪除,才能執行其他表的語句
用戶表 角色表 中間表 三張表
順序刪除規則:先刪除 中間表 (存在外鍵約束) 在執行主表 或 從表:存在外鍵約束,不能 執行SQL語句,
對策:
在從表 加上@ManyToMany(mappedBy = "role",fetch = FetchType.EAGER)
主表加上@ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
執行結果:
1 com.wsc.core.onetone.ManyToMany,deleteUser 2 log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner). 3 Hibernate: select user0_.user_id as user_id1_6_0_, user0_.password as password2_6_0_, user0_.username as username3_6_0_, role1_.userid as userid1_7_1_, role2_.role_id as roleid2_7_1_, role2_.role_id as role_id1_5_2_, role2_.memo as memo2_5_2_, role2_.role_name as role_nam3_5_2_ from user user0_ left outer join user_role role1_ on user0_.user_id=role1_.userid left outer join role role2_ on role1_.roleid=role2_.role_id where user0_.user_id=? 4 Hibernate: select user0_.roleid as roleid2_7_0_, user0_.userid as userid1_7_0_, user1_.user_id as user_id1_6_1_, user1_.password as password2_6_1_, user1_.username as username3_6_1_ from user_role user0_ inner join user user1_ on user0_.userid=user1_.user_id where user0_.roleid=? 5 Hibernate: select role0_.userid as userid1_7_0_, role0_.roleid as roleid2_7_0_, role1_.role_id as role_id1_5_1_, role1_.memo as memo2_5_1_, role1_.role_name as role_nam3_5_1_ from user_role role0_ inner join role role1_ on role0_.roleid=role1_.role_id where role0_.userid=? 6 Hibernate: select user0_.roleid as roleid2_7_0_, user0_.userid as userid1_7_0_, user1_.user_id as user_id1_6_1_, user1_.password as password2_6_1_, user1_.username as username3_6_1_ from user_role user0_ inner join user user1_ on user0_.userid=user1_.user_id where user0_.roleid=? 7 8 9 Hibernate: delete from user_role where userid=? 10 Hibernate: delete from user where user_id=? // 僅執行 兩個刪除語句,刪除單表id OK!!! 11 12 13 log4j:WARN Please initialize the log4j system properly. 14 15 Process finished with exit code 0
2....想要刪除 兩張表 :必須刪除所有你要刪除的數據存在的外鍵約束,才能執行OK。
中間表 約束 id (兩個表)
A(Id 主表) B (id 從表)
2 1
1 2
1 3
所以:要刪除 id=2 ,必須刪除 id=1 的 id=3 的也要刪除。
執行方法:delete(1),delete(2),delete(3),就是全刪除。