2016-07-15 48 views
0

我想注册一个新的部门,我得到这个错误嵌套的例外是org.hibernate.HibernateException:无会话发现当前线程addDepartment控制器

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注解

@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); 
    } 

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

+0

你要检查您在有像'在hibernate.cfg.xml中的螺纹'或'<支撑键= “的hibernate.current_session_context_class”>螺纹'在Spring配置中定义'SessionFactoryBean'。 –

+0

我没有hibernate.cfg.xml文件 – Blaze

+0

无论是在Spring XML配置文件还是在用@ @ Configuration注解的Java类中,肯定有一组Hibernate属性? –

回答

0

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

如果您使用Propagation.REQUIRED,会发生同样的情况吗?

+0

这解决了我的问题 – Blaze

相关问题