Spring-Hibernate – 找不到当前线程的Session

我有一个使用spring和hibernate的java stuts2 web应用程序。

我得到org.hibernate.HibernateException: No Session found for current thread

SpringBean.xml

                 org.hibernate.dialect.MySQLDialect true update     hibernate.cfg.xml    <!--  -->           

的hibernate.cfg.xml

         

CustomerServiceImpl.java

 package org.rohith.service.impl; import org.rohith.dao.impl.CustomerDaoImpl; import org.rohith.model.Customer; import org.rohith.service.CustomerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class CustomerServiceImpl implements CustomerService { @Autowired private CustomerDaoImpl customerDaoImpl; @Override public void saveCustomer(Customer customer) { customerDaoImpl.saveCustomer(customer); } public CustomerDaoImpl getCustomerDaoImpl() { return customerDaoImpl; } public void setCustomerDaoImpl(CustomerDaoImpl customerDaoImpl) { this.customerDaoImpl = customerDaoImpl; } } 

CustomerDaoImpl.java

 package org.rohith.dao.impl; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.rohith.dao.CustomerDao; import org.rohith.model.Customer; public class CustomerDaoImpl implements CustomerDao { private SessionFactory sessionFactory; @Override public void saveCustomer(Customer customer) { Session session = getSession(); session.clear(); try { session.saveOrUpdate(customer); session.flush(); } catch (Throwable e) { throw new RuntimeException(e.getMessage(), e); } } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public SessionFactory getSessionFactory() { return this.sessionFactory; } public Session getSession() throws HibernateException { Session sess = getSessionFactory().getCurrentSession(); if (sess == null) { sess = getSessionFactory().openSession(); } // Session sess = getSessionFactory().openSession(); return sess; } } 

CustomerAction.java

 public class CustomerAction extends ActionSupport{ private String name; private String addr1; private String addr2; private String city; private String state; private CustomerServiceImpl customerServiceImpl; //Getters and setters public String execute(){ Customer cust= new Customer(); cust.setName(name); cust.setAddress1(addr1); cust.setAddress2(addr2); cust.setCity(city); cust.setState(state); System.out.println(name); WebApplicationContext webApplicationContext = WebApplicationContextUtils .getRequiredWebApplicationContext(getRequest().getSession() .getServletContext()); customerServiceImpl = (CustomerServiceImpl) webApplicationContext.getBean("customerServiceImpl"); customerServiceImpl.saveCustomer(cust); //saveCust(cust); return "success"; } protected HttpServletRequest getRequest() { return ServletActionContext.getRequest(); } protected HttpServletResponse getResponse() { return ServletActionContext.getResponse(); } } 

我得到的例外

 org.hibernate.HibernateException: No Session found for current thread org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:978) org.rohith.dao.impl.CustomerDaoImpl.getSession(CustomerDaoImpl.java:33) org.rohith.dao.impl.CustomerDaoImpl.saveCustomer(CustomerDaoImpl.java:16) org.rohith.service.impl.CustomerServiceImpl.saveCustomer(CustomerServiceImpl.java:18) org.rohith.CustomerAction.execute(CustomerAction.java:36) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) 

您在Spring配置中指定了事务管理器,但没有关于何时或何处应用事务的配置。

SpringBean.xml中,您应该取消注释

  

然后你应该将CustomerServiceImpl.saveCustomer方法注释为@Transactional

 @Service public class CustomerServiceImpl implements CustomerService { ... @Override @Transactional public void saveCustomer(Customer customer) { customerDaoImpl.saveCustomer(customer); } ... } 

hibernate.cfg.xml 4的hibernate.cfg.xml文件中添加以下属性。

 thread 

如果您的配置基于没有xml的注释,请在java配置文件中添加@EnableTransactionManagement注释。

这可能对某人有帮助

 You can add in hibernate.proerties below value. It's worked for me. hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext 

要么

的hibernate.current_session_context_class =螺纹