could not execute statement; SQL [n/a]; constraint [null] (多对多表报错不能执行SQL语句)

时间:2019-08-21
本文章向大家介绍could not execute statement; SQL [n/a]; constraint [null] (多对多表报错不能执行SQL语句),主要包括could not execute statement; SQL [n/a]; constraint [null] (多对多表报错不能执行SQL语句)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

多表删除报错:

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),就是全删除。

原文地址:https://www.cnblogs.com/wangshichang/p/11390933.html