0
我試圖使在Hibernate中的事務持久化過程中發生的約束違規錯誤更有意義。有沒有辦法將HibernateException轉換爲其他內容,然後是Spring中的DataAccessException
我看到如何使用HibernateExceptions干擾的唯一方法是通過PersistenceExceptionTranslator這個春天,
package org.springframework.dao.support;
import org.springframework.dao.DataAccessException;
public interface PersistenceExceptionTranslator {
DataAccessException translateExceptionIfPossible(RuntimeException ex);
}
它轉換休眠例外的DataAccessExceptions,但是這意味着我需要從繼承的DataAccessExceptions我的異常層次結構。
是否有任何其他自然的方式來做到這一點,而不會在Exception繼承層次結構中引入DataAceessExceptions?
不是太好,PersistenceExceptionTranslator更好:) – mavarazy
我知道,但你問了不同的解決方案,所以我給你帶來了一個:)此外,你將不必延長'DataAccessException' –
也許你想要使用'@ ExceptionHandler'?請參閱http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-exceptionhandlers –