删除记录期间处理错误
handle error during remove record
void deleteFilm(@PathVariable(value = "id") Integer id) {
try {
filmService.deleteFilm(id);
}
catch (ConstraintViolationException e) {
throw e;
}
catch (SQLIntegrityConstraintViolationException ex) {
}
}
2018-08-15 18:12:10.075 WARN 8568 --- [io-8080-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1451, SQLState: 23000
2018-08-15 18:12:10.075 ERROR 8568 --- [io-8080-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper : Cannot delete or update a parent row: a foreign key constraint fails (`todo`.`seance`, CONSTRAINT `FKchlcmip8ejlfuo4c990k5ry8y` FOREIGN KEY (`film_id`) REFERENCES `film` (`id`))
2018-08-15 18:12:10.077 INFO 8568 --- [io-8080-exec-10] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
2018-08-15 18:12:10.080 ERROR 8568 --- [io-8080-exec-10] o.h.i.ExceptionMapperStandardImpl : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
2018-08-15 18:12:10.154 ERROR 8568 --- [io-8080-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] 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
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`todo`.`seance`, CONSTRAINT `FKchlcmip8ejlfuo4c990k5ry8y` FOREIGN KEY (`film_id`) REFERENCES `film` (`id`))
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_131]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131]
ConstraintViolationException 不捕获错误并且我使用的 SQLIntegrityConstraintViolationException 永远不会被相应的 try 块抛出
我读到
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.sql.SQLException
java.sql.SQLNonTransientException
java.sql.SQLIntegrityConstraintViolationException
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
jdbc4.MySQLIntegrityConstraintViolationException 继承自 SQLEXception 但此异常永远不会被 try 块抛出
这是一个编程错误,必须通过选择如何处理外键来更正。
问题是您要删除父记录,留下孤立条目(film_id
in todo.seance
,引用电影 ID)
你有两个选择
- 做级联删除,这样一部电影被删除,相应的
seance
条记录也被删除(数据库自动完成)。 MySQL 关于外键的文档是 here(带有参考选项,其中包括 on delete cascade
,等等)
- 更改应用程序逻辑以在删除父电影记录之前先删除
film_id
的 seance
个条目。
记住你需要显式抛出异常:
catch (SQLIntegrityConstraintViolationException ex) {
//Assuming deleteFilm() has the correct throws clause
throw ex; //you are not doing this.
}
void deleteFilm(@PathVariable(value = "id") Integer id) {
try {
filmService.deleteFilm(id);
}
catch (ConstraintViolationException e) {
throw e;
}
catch (SQLIntegrityConstraintViolationException ex) {
}
}
2018-08-15 18:12:10.075 WARN 8568 --- [io-8080-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1451, SQLState: 23000
2018-08-15 18:12:10.075 ERROR 8568 --- [io-8080-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper : Cannot delete or update a parent row: a foreign key constraint fails (`todo`.`seance`, CONSTRAINT `FKchlcmip8ejlfuo4c990k5ry8y` FOREIGN KEY (`film_id`) REFERENCES `film` (`id`))
2018-08-15 18:12:10.077 INFO 8568 --- [io-8080-exec-10] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
2018-08-15 18:12:10.080 ERROR 8568 --- [io-8080-exec-10] o.h.i.ExceptionMapperStandardImpl : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
2018-08-15 18:12:10.154 ERROR 8568 --- [io-8080-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] 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
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`todo`.`seance`, CONSTRAINT `FKchlcmip8ejlfuo4c990k5ry8y` FOREIGN KEY (`film_id`) REFERENCES `film` (`id`))
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_131]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131]
ConstraintViolationException 不捕获错误并且我使用的 SQLIntegrityConstraintViolationException 永远不会被相应的 try 块抛出
我读到
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.sql.SQLException
java.sql.SQLNonTransientException
java.sql.SQLIntegrityConstraintViolationException
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
jdbc4.MySQLIntegrityConstraintViolationException 继承自 SQLEXception 但此异常永远不会被 try 块抛出
这是一个编程错误,必须通过选择如何处理外键来更正。
问题是您要删除父记录,留下孤立条目(film_id
in todo.seance
,引用电影 ID)
你有两个选择
- 做级联删除,这样一部电影被删除,相应的
seance
条记录也被删除(数据库自动完成)。 MySQL 关于外键的文档是 here(带有参考选项,其中包括on delete cascade
,等等) - 更改应用程序逻辑以在删除父电影记录之前先删除
film_id
的seance
个条目。
记住你需要显式抛出异常:
catch (SQLIntegrityConstraintViolationException ex) {
//Assuming deleteFilm() has the correct throws clause
throw ex; //you are not doing this.
}