I am using Spring3 and Hibernate4. I have below interface and classes.
public interface FetchDao{
List<FetchEntity> getOrderList(Integer orderNumber);
}
public class FetchDaoImpl implements FetchDao{
@Autowired
private SessionFactory sessionFactory; //sessionFactory is injected through spring
@Override
public List<FetchEntity> getOrderList(Integer orderNumber) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
FetchEntity.class);
criteria.add(Restrictions.eq("orderNumber", orderNumber));
return criteria.list();
}
}
Now above configuration works fine without any issues. I have placed them in DAO layer. Above method does not throw any exception. But is it good practice to throw user defined exception always? as below:-
public interface FetchDao{
List<FetchEntity> getOrderList(Integer orderNumber) **throws CustomDAOException**;
}
public class FetchDaoImpl implements FetchDao{
@Autowired
private SessionFactory sessionFactory; //sessionFactory is injected through spring
@Override
public List<FetchEntity> getOrderList(Integer orderNumber) **throws CustomDAOException**{
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
FetchEntity.class);
criteria.add(Restrictions.eq("orderNumber", orderNumber));
return criteria.list();
}
}
Here CustomDAOException
is user defined exception.
Thanks!
Yes it is good to throw user-defined exception from dao layer,so that your code can get more modularity.