@ExceptionHandler不适用于特定的hibernate异常

时间:2018-05-08 10:51:05

标签: java spring hibernate uitableview exception-handling

我正在尝试捕获一些内部异常,然后提示最终用户使用更好的消息。但是当谈到这个例外时:

  

异常StackTrace

08/May/2018:20:39:09.947 +0800 ERROR o.h.e.j.spi.SqlExceptionHelper [] ERROR: update or delete on table "alert_rule" violates foreign key constraint "alert_alert_rule_id_fkey" on table "alert"
  Detail: Key (id)=(58) is still referenced from table "alert".
08/May/2018:20:39:09.977 +0800 ERROR 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 [alert_alert_rule_id_fkey]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
org.postgresql.util.PSQLException: ERROR: update or delete on table "alert_rule" violates foreign key constraint "alert_alert_rule_id_fkey" on table "alert"
  Detail: Key (id)=(58) is still referenced from table "alert".

@ExceptionHandler(value = {javax.validation.ConstraintViolationException.class, org.hibernate.exception.ConstraintViolationException.class})似乎没有按预期工作。

我刚检查了这个post,指出在与常规处理程序一起使用时应该处理特定异常处理程序。

post也可以在一个处理程序中处理多个异常。

@RestControllerAdvice
@Slf4j
public class GlobalControllerExceptionHandler {

    @ExceptionHandler(value = {javax.validation.ConstraintViolationException.class,
            org.hibernate.exception.ConstraintViolationException.class})
    public void constraintViolationException(Exception ex,
                                                      WebRequest req) {
        log.error("Constraint violation while processing {}", req.toString());
    }



    @ExceptionHandler(value = {Exception.class})
    public void unknownException(Exception ex, WebRequest req) {
        log.error("Unknown Internal Exception happens");
    }
}

是否有我错过的东西,感谢任何有用的细节。

1 个答案:

答案 0 :(得分:1)

您正在尝试处理void OnPropertyChanged(object sender, PropertyChangedEventArgs eventArgs) { if(!(eventArgs is PropertyChangedProgrammaticallyEventArgs )) { // log user interaction } } ,但您的异常处理程序仅处理DataIntegrityViolationException

将异常类型添加到处理程序:

ConstraintViolationException