title: Estado HTTP 500 - Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread
body: <p>I am using Spring with hibernate newbie, I followed step by step tutorials but I get this session error but do not know exactly how to fix it, can anyone help me? Thank you very much.
  A contintuacion I write gives me the error by the code I wrote.</p>
  
  <p>Grave: El Servlet.service() para el servlet [springapp] en el contexto con ruta [/springapp] lanzó la excepción [Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread] con causa raíz
  org.hibernate.HibernateException: No Session found for current thread
      at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
      at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:978)
      at com.altobri.conta.dao.EmpresasDAOImpl.listEmpresas(EmpresasDAOImpl.java:43)
      at com.altobri.conta.service.EmpresasServiceImpl.listEmpresas(EmpresasServiceImpl.java:47)
      at com.altobri.conta.controller.empresaListaController.list(empresaListaController.java:30)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:744)</p>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt;
   &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   id="WebApp_ID" version="2.5"&gt;
   &lt;display-name&gt;Spring3MVC&lt;/display-name&gt;
   &lt;welcome-file-list&gt;
       &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
   &lt;/welcome-file-list&gt;
  
   &lt;servlet&gt;
  </code></pre>
  
  <p><strong>springapp-servlet.xml</strong></p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:p="http://www.springframework.org/schema/p"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans     
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
     &lt;context:component-scan base-package="com.companyname.springapp.web" /&gt;
     &lt;context:component-scan base-package="com.springHibernate" /&gt;
     &lt;context:component-scan base-package="com.altobri.conta.*" /&gt;
  
   &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
          &lt;property name="viewClass"
              value="org.springframework.web.servlet.view.JstlView" /&gt;
          &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
  
  
       &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:3306/empresas" /&gt;
      &lt;property name="username" value="root" /&gt;
      &lt;property name="password" value="" /&gt;
    &lt;/bean&gt;
  
    &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
       &lt;property name="annotatedClasses"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.altobri.conta.model.Empresas&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
        &lt;props&gt;
          &lt;prop 
           key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
          &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
        &lt;/props&gt;
      &lt;/property&gt;
    &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
      p:sessionFactory-ref="sessionFactory"&gt;
    &lt;/bean&gt;
  &lt;/beans&gt;
  
  
  
   &lt;servlet-name&gt;springapp&lt;/servlet-name&gt;
      &lt;servlet-class&gt;
          org.springframework.web.servlet.DispatcherServlet
      &lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;springapp&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>empresaListaController.java</strong></p>
  
  <pre><code>package com.altobri.conta.controller;
  
  import java.util.List;
  import java.util.Map;
  
  import org.apache.log4j.Logger;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.ui.Model;
  import org.springframework.ui.ModelMap;
  import org.springframework.web.servlet.ModelAndView;
  
  import com.altobri.conta.model.Empresas;
  import com.altobri.conta.service.*;
  
  @Controller
  public class empresaListaController {
  
      @Autowired
      private EmpresasService empresasService;
      protected final Logger logger = Logger.getLogger(getClass());
  
  
      @RequestMapping("/empresaLista")
      // @RequestMapping(method = RequestMethod.GET)
      public String list(Model model) {
  
        List&lt;Empresas&gt; empresas = empresasService.listEmpresas();
        model.addAttribute("empresalist", empresas);
        return "empresaLista";
      }
  }
  </code></pre>
  
  <p><strong>EmpresasDAO</strong></p>
  
  <pre><code>package com.altobri.conta.dao;
  
  import com.altobri.conta.model.Empresas;
  import java.util.List;
  
  public interface EmpresasDAO {
      void persistEmpresas(Empresas empresas);
  
      Empresas findEmpresasById(int clave);
  
      void updateEmpresas(Empresas empresas);
  
      void deleteEmpresas(Empresas empresas);
  
      public List&lt;Empresas&gt; listEmpresas();
  
  }
  </code></pre>
  
  <p><strong>EmpresasDAOImpl.java</strong></p>
  
  <pre><code>package com.altobri.conta.dao;
  
  import java.util.List;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Repository;
  
  import com.altobri.conta.model.Empresas;
  
  @Repository("empresasDAO")
  public class EmpresasDAOImpl implements EmpresasDAO {
  
      @Autowired
      public SessionFactory sessionFactory;
  
      @Override
      public void persistEmpresas(Empresas empresas) {
          sessionFactory.getCurrentSession().persist(empresas);
      }
  
      @Override
      public Empresas findEmpresasById(int clave) {
          return (Empresas) sessionFactory.getCurrentSession().get(
                  Empresas.class, clave);
      }
  
      @Override
      public void updateEmpresas(Empresas empresas) {
          sessionFactory.getCurrentSession().update(empresas);
  
      }
  
      @Override
      public void deleteEmpresas(Empresas empresas) {
          sessionFactory.getCurrentSession().delete(empresas);
  
      }
  
      @Override
      public List&lt;Empresas&gt; listEmpresas() {
          Session session = sessionFactory.getCurrentSession();
          List empresas = session.createQuery("from Empresas").list();
          // TODO Auto-generated method stub
           return empresas;
      }
  
  }
  </code></pre>
  
  <p><strong>Empresas.java</strong></p>
  
  <pre><code>package com.altobri.conta.model;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  @Entity
  @Table(name = "EMPRESAS")
  public class Empresas {
  
      @Id
      @Column(name = "CLAVE", nullable = false)
      private int clave;
  
      @Column(name = "NOMBRE", nullable = false)
      private String nombre;
  
  
      public Empresas() {
  
  
      }
  
  
      public int getClave() {
          return clave;
      }
  
  
      public void setClave(int clave) {
          this.clave = clave;
      }
  
  
      public String getNombre() {
          return nombre;
      }
  
  
      public void setNombre(String nombre) {
          this.nombre = nombre;
      }
  
  
  
  }
  </code></pre>
  
  <p><strong>EmpresasService.java</strong></p>
  
  <pre><code>package com.altobri.conta.service;
  
  
  
  import java.util.List;
  
  import com.altobri.conta.model.Empresas;
  
  public interface EmpresasService {
  
      void persistEmpresas(Empresas empresas);
  
      Empresas findEmpresasById(int clave);
  
      void updateEmpresas(Empresas empresas);
  
      void deleteEmpresas(Empresas empresas);
  
      public List&lt;Empresas&gt; listEmpresas();
  }
  </code></pre>
  
  <p><strong>EmpresasServiceImpl.java</strong></p>
  
  <pre><code>package com.altobri.conta.service;
  
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.altobri.conta.dao.EmpresasDAO;
  import com.altobri.conta.model.Empresas;
  
  @Service("empresasService")
  public class EmpresasServiceImpl implements EmpresasService{
  
      @Autowired
      EmpresasDAO empresasDAO;
  
      @Override
      @Transactional
      public void persistEmpresas(Empresas empresas) {
          empresasDAO.persistEmpresas(empresas);
  
      }
  
      @Override
      @Transactional
      public void updateEmpresas(Empresas empresas) {
          empresasDAO.updateEmpresas(empresas);
  
      }
      @Override
      @Transactional
      public Empresas findEmpresasById(int clave) {
          return empresasDAO.findEmpresasById(clave);
      }
  
      @Override
      @Transactional
      public void deleteEmpresas(Empresas empresas) {
          empresasDAO.deleteEmpresas(empresas);
  
      }
  
      @Override
      public List&lt;Empresas&gt; listEmpresas() {
          // TODO Auto-generated method stub
          return empresasDAO.listEmpresas();
      }
  
  }
  </code></pre>