2016-07-15 45 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

相關問題