Multi-table delete error:
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`))
Analysis: you want to delete a single table data error
My error was in when deleted: the main foreign key constraint middle of the table to delete, you can perform other statements table
Intermediate user roles table table table three tables
Delete rule sequence: delete intermediate table (foreign key constraints) in the execution of the main table or from the table: foreign key constraints, can not execute SQL statements,
Solution:
In the table plus @ManyToMany (the mappedBy = "Role",FETCH = FetchType.EAGER)
Primary table plus @ManyToMany (= CascadeType.ALL in Cascade, FETCH = FetchType.EAGER )
Results of the:
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 .... want to delete the two tables: You must remove all foreign key constraints exist that you want to delete the data, to perform OK.
Intermediate table constraint id (two tables)
A (Id primary table) B (id from a table)
2 1
1 2
1 3
So: To delete id = 2, you must remove id = id 1 is also deleted = 3.
Execution method: delete (1), delete (2), delete (3), is the whole deleted.