嵌套异常是org.hibernate.HibernateException:找不到当前线程addDepartment控制器

时间:2016-07-15 16:04:42

标签: java spring hibernate spring-mvc

我正在尝试注册一个新的部门而且我收到此错误

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread

我确保将其添加到我的xml文件中并且错误仍然存​​在

<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
    <!-- Scans within the base package of the application for @Component classes to configure as beans -->
    <context:component-scan base-package="com.xxx.account.*"/>

这是抛出异常的控制器方法

@RequestMapping(value = "/addDepartment", method = RequestMethod.GET)
    public ModelAndView addCategory(@ModelAttribute("command")  Department department,
            BindingResult result) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("department",  departmentService.getDepartments());
        return new ModelAndView("addDepartment");
    }

这是我尝试访问控制器方法时遇到的错误的完整堆栈跟踪

org.hibernate.HibernateException: No Session found for current thread
    org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)   org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014) m.chuma.account.dao.DepartmentDaoImpl.getDeparments(DepartmentDaoImpl.java:31)com.chuma.account.service.impl.DepartmentServiceImpl.getDepartments(DepartmentServiceImpl.java:28)

这是使用@transactional注释

注释的部门dao
@Service("departmentService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class DepartmentServiceImpl implements DepartmentService{


    @Autowired
    private DepartmentDao departmentDao;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void addDepartment(Department department) {
        departmentDao.addDepartment(department);
    }

到目前为止,我的调试似乎一切正常,但是当我尝试访问控制器方法以返回视图时,错误仍然存​​在。什么可能是错的或我错过了什么?

1 个答案:

答案 0 :(得分:0)

为什么你的@Transactional定义没有传播= Propagation.SUPPORTS? 这意味着只有在您调用方法的位置已经打开了一个事务时,该方法才会在事务上下文中运行。

如果你使用Propagation.REQUIRED会发生同样的事情吗?