有没有办法将HibernateException转换为别的东西,然后在Spring中DataAccessException [英] Is there a way to translate HibernateException to something else, then DataAccessException in Spring

查看:133
本文介绍了有没有办法将HibernateException转换为别的东西,然后在Spring中DataAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我看到如何干预HibernateExceptions的唯一方法是通过在Hibernate中的事务持久化中发生约束违规错误。 Spring中的PersistenceExceptionTranslator

  package org.springframework.dao.support; 

import org.springframework.dao.DataAccessException;

public interface PersistenceExceptionTranslator {

DataAccessException translateExceptionIfPossible(RuntimeException ex);

}

将Hibernate异常转换为DataAccessExceptions,需要从DataAccessExceptions继承我的异常层次结构。



有没有其他自然的方法来做,没有在Exception继承层次结构中引入DataAceessExceptions? / em>

解决方案

您可以捕获 HibernateException 从您的存储库方法将其作为检查异常重新抛出。但是,你必须处理上层的异常,但你也可以简单地把它抛在代码层之外。


I am trying to make more sense out of constraint violation errors, happening during transaction persistence in Hibernate.

The only way I see how to interfere with HibernateExceptions is through PersistenceExceptionTranslator in spring,

package org.springframework.dao.support;

import org.springframework.dao.DataAccessException;

public interface PersistenceExceptionTranslator {

    DataAccessException translateExceptionIfPossible(RuntimeException ex);

}

which translates Hibernate exceptions to DataAccessExceptions, but that means I'll need to inherit my exception hierarchy from DataAccessExceptions.

Is there any other natural way to do this, without introducing DataAceessExceptions in Exception inheritance hierarchy?

解决方案

You can catch HibernateException and rethrow it as a checked exception from your repository method. But then you will have to deal with the exception in the upper layers, but you also can simply keep throwing it outside your code layer.

这篇关于有没有办法将HibernateException转换为别的东西,然后在Spring中DataAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆