有没有办法将HibernateException转换为别的东西,然后在Spring中DataAccessException [英] Is there a way to translate HibernateException to something else, then DataAccessException in Spring
问题描述
我看到如何干预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屋!