0
執行JUnit模擬測試時,此代碼失敗。方法定義中的異常(NoResultException,PersistenceException)與方法實際返回的異常(UnknownEntityException,ServiceException)不匹配。這只是一個明顯的錯誤?處理存儲庫層中的異常
項目異常(UnknownEntityException,ServiceException)被認爲是創建的,所以會有一種常見的方式來處理異常。原始代碼的創建者早已消失,我無法確定這是否是一個好主意。
@Repository
public class InstitutionRepository {
@PersistenceContext
private EntityManager entityManager;
public EntityInstitutionModel findInstitutionByName(String instName)
throws NoResultException, PersistenceException {
try {
Query query = null;
query = entityManager.createQuery("SELECT ex from EntityInstitutionModel ex where ex.instname = :instName");
query.setParameter("instName", instName);
EntityInstitutionModel instModel = (EntityInstitutionModel) query.getSingleResult();
System.out.println("InstitutionRepository.findInstitutionByName.getId() = " + instModel.getId());
return instModel;
}catch (NoResultException e) {
throw new UnknownEntityException(e);
} catch (Exception e) {
throw new ServiceException(e);
}
}
}