Exception translation:-
When we use Hibernate, JPA, or JDO in a DAO, you must decide how
to handle the persistence technology’s native exception classes. The DAO throws
a subclass of a HibernateException, PersistenceException or
JDOException depending on the technology. These exceptions are all run-time
exceptions and do not have to be declared or caught. You may also have to deal
with IllegalArgumentException and IllegalStateException. This
means that callers can only treat exceptions as generally fatal, unless they
want to depend on the persistence technology’s own exception structure.Spring
enables exception translation to be applied transparently through the
@Repository annotation:
@Repository
public class ProductDaoImpl implements ProductDao {
// class body here...
}
<beans>
<!-- Exception translation bean post processor -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="myProductDao" class="product.ProductDaoImpl"/>
</beans>
The postprocessor automatically looks for all exception translators (implementations of the PersistenceExceptionTranslator interface) and advises all beans marked with the @Repository annotation so that the discovered translators can intercept and apply the appropriate translation on the thrown exceptions.