【发布时间】:2015-08-19 18:33:53
【问题描述】:
我已经使用 controller->service->dao 架构构建了一个 spring mvc 应用程序。 DAO 对象正在使用休眠。服务注释为@Transactional。
我正在尝试在服务中捕获 dao 异常,将它们包装起来,然后将它们扔给我的控制器:
服务
@Override
public Entity createEntity(Entity ent) throws ServiceException {
try {
return entityDAO.createEntity(ent);
} catch (DataAccessException dae) {
LOG.error("Unable to create entity", dae);
throw new ServiceException("We were unable to create the entity for the moment. Please try again later.", dae);
}
}
控制器
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String createEntity(@ModelAttribute(value = "newEntity") Entity newEntity, RedirectAttributes redirectAttributes) {
try {
entityService.createEntity(newEntity);
} catch (ServiceException se) {
redirectAttributes.addFlashAttribute("error", se.getMessage());
}
}
return "redirect:/entity/manage";
}
但是,即使 DataAccessException 在服务级别被捕获,它也会以某种方式不断冒泡到我的控制器。
例如,如果我不满足数据库级别的唯一字段条件,我会收到 HTTP 错误 500,其中包含以下内容:
org.hibernate.AssertionFailure: null id in com.garmin.pto.domain.Entity entry (don't flush the Session after an exception occurs)
【问题讨论】:
标签: spring hibernate spring-mvc