简体   繁体   English

Hibernate异常继续由RuntimeException.class而不是HibernateException.class处理

[英]Hibernate exceptions keep getting handled by RuntimeException.class rather than HibernateException.class

I have a ControllerAdvice set up, with two exception handlers; 我有一个ControllerAdvice设置,有两个异常处理程序; one for any Hibernate exception (HibernateException.class) and one for any other Runtime exception (RuntimeException.class) 一个用于任何Hibernate异常(HibernateException.class),一个用于任何其他运行时异常(RuntimeException.class)

@ExceptionHandler(HibernateException.class)
public String handleHibernateException(HibernateException exc, Model theModel) {

    String message = "An error has occured: " + exc.getLocalizedMessage() + "\r\n";

    myLogger.warning(message);

    theModel.addAttribute("exception", message);

    return "testing";
}


@ExceptionHandler(RuntimeException.class)
public String handleRuntimeExceptions(RuntimeException exc, Model theModel) {

    if (exc instanceof HibernateException) {

        return handleHibernateException((HibernateException) exc.getCause(), theModel);
    }   

    String message = "An error has occured: " + exc.getLocalizedMessage() + "\n"
            + exc.getCause().getCause().toString() + "\r\n";
    myLogger.warning(message);

    theModel.addAttribute("exception", message);

    return "testing";

I'm testing this out by messing up my query statements, which gives me a QuerySyntaxException; 我正在通过搞乱我的查询语句来测试它,这给了我一个QuerySyntaxException; a subclass of HibernateException. HibernateException的子类。 And yet, it is still handled by my Runtime exception method. 然而,它仍由我的运行时异常方法处理。

我认为@ExceptionHandler完全匹配了QuerySyntaxException的Exact Exception名称。您可以尝试使用QuerySyntaxException。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @Transactional(noRollbackFor = RuntimeException.class)不会阻止RuntimeException上的回滚 - @Transactional (noRollbackFor=RuntimeException.class) does not prevent rollback on RuntimeException 为什么hibernate将HibernateException更改为(未选中)RuntimeException - why hibernate changed HibernateException to (unchecked) RuntimeException 获取用于MethodInvocation而不是声明类的实际类 - Getting the actual class used for a MethodInvocation rather than the declaring class org / hibernate / HibernateException:不支持的major.minor版本52.0(无法加载org.hibernate.HibernateException类) - org/hibernate/HibernateException : Unsupported major.minor version 52.0 (unable to load class org.hibernate.HibernateException) 在包装类的情况下如何处理异常? - How should exceptions be handled in case of wrapper class? HSQL - 休眠。 org.hibernate.HibernateException:“…Author.class”实例的标识符从 1 更改为 null - HSQL - HIBERNATE. org.hibernate.HibernateException: identifier of an instance of “…Author.class” was altered from 1 to null Hibernate没有映射类 - Class not getting mapped with Hibernate 获取 org.hibernate.HibernateException: 没有配置 CurrentSessionContext - Getting org.hibernate.HibernateException: No CurrentSessionContext configured 使用我的课程,而不是使用JAR - Use my class rather than from a JAR 变量而不是EclipseLink中的类的@AdditionalCriteria - @AdditionalCriteria on a variable rather than a class in EclipseLink
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM