If you do want your DAOs to throw Spring DataAccessExceptions there are two simple things to do.
- Add the @Repository annotation to your DAO impl.
- Add a PersistenceExceptionTranslationPostProcessor bean to your Spring setup.
What this does is tell Spring to wrap an interceptor around your DAO bean that handles the exception translation.
Looking at the UserDaoImpl from the past article...
@Repository
public class UserDaoImpl extends AbstractHibernateDao<User> implements UserDao {
public UserDaoImpl(SessionFactory sessionFactory) {
super(User.class, sessionFactory);
}
...
}
Then in your Spring application context.xml you simply add one line...
Now, this pattern I present is somewhat old school. There is all this fancy component scanning stuff you can do with the Spring 2.5 XML namespaces. Honestly the amount of voodoo that goes on there freaks me out a bit. You can study that on your own if you'd like. A good place to start is here.