Let's consider the following architecture:
Services use DAOs to manipulate Domain Objects. Services make all efforts to be really isolated (and agnostic) from data access technology.
But, when DAOs are specifically Hibernate DAOs, lazy loading proxies get into the way, so any HibernateException occurred during lazy loading will not be translated, and will leak into Service classes :!: (see code below)
I am having big trouble trying to make DAOs and Services really reusable and properly isolated. OK, I could install interceptors here and there to translate leaked HibernateExceptions back into DataAccessExceptions... But I think it begins to get messy... :roll:
So I'm asking your help, thanks in advance!
public interface CustomerService
Customer login(String id, String password) throws LoginException;
public class CustomerServiceImpl implements CustomerService
private CustomerDAO customerDAO;
public setCustomerDAO(CustomerDAO customerDAO)
this.customerDAO = customerDAO;
public Customer login(String id, String password) throws LoginException
Customer customer = customerDAO.findById(id);
// verify password and throw LoginException on failure
// but hey, a database failure on loading any lazy
// loaded Customer property (not only collections, but any
// Hibernate3 lazy loaded instance) will result on HibernateException,
// thus leaking technology specific Exceptions into Service Layer
// and also breaking DataAccessException philosophy...!
public interface CustomerDAO
Customer findById(String id) throws DataAccessException;
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDAO
public Customer findById(String id) throws DataAccessException
return (Customer)getHibernateTemplate().load(Customer.class, id);