Hello!
I study Spring and the documentation says that all @Repository class exceptions will be automatically translated to a DataAccessException.
User Repository:
package org.example.dao; public interface UserDAO { void addUser(User user); } @Repository public class UserDAOImpl implements UserDAO { public void addUser(User user) { throw new HibernateException("unchecked exception"); } } Service for User:
package org.example.services; public interface UserService { void addUser(User user); } @Service public class UserServiceImpl implements UserService { @Autowired private UserDAO userDAO; @Override public void addUser(User user) { try { userDAO.addUser(user); } catch (Exception e) { e.printStackTrace(); } } } In web.xml:
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> In fact, I get a HibernateException. I do not understand how exceptions broadcast works. Why did not HibernateException be translated into an exception of type DataAccessException?
Who can tell me what I understood wrong? :)
DataAccessExceptionis belowHibernateException, and you catchExceptionthatDataAccessException- and you explicitly specify theHibernateExceptionerror. - AndDataAccessException. - AndHibernateExceptionextendsRuntimeException. andDataAccessExceptionextendsNestedRuntimeException, and alreadyNestedRuntimeExceptionextendsRuntimeException, andRuntimeExceptionextendsException. - And