我正在使用 Spring3 和 Hibernate4。我有以下接口和类。
FetchDao.java
public interface FetchDao{
List<FetchEntity> getOrderList(Integer orderNumber);
}
FetchDaoImpl.java
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();
}
}
现在上面的配置工作正常,没有任何问题。我已将它们放在 DAO 层中。上面的方法不会抛出任何异常。但是总是抛出用户定义的异常是一种好习惯吗?如下:-
FetchDao.java
public interface FetchDao{
List<FetchEntity> getOrderList(Integer orderNumber) **throws CustomDAOException**;
}
FetchDaoImpl.java
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();
}
}
这CustomDAOException
是用户定义的异常。
谢谢!