title: No Session found for current thread (HIBERNATE)
body: <p><strong>OK JAVA guru ))</strong></p>
  <p>I really new to spring and I got into this trouble with &quot;No session found the current thread&quot; when I refer to the following page JSP security after login. Up to this point everything is working.</p>
  <p><strong>I need your help guys!!!!!</strong></p>
  <p>Here is my controller for that page:</p>
  <pre><code>package employee.controller;
  
  import employee.service.AdminService;
  import employee.service.UserService;
  import org.apache.log4j.Logger;
  import org.springframework.security.access.annotation.Secured;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  
  import javax.annotation.Resource;
  
  /**
   * @author serge
   *
   * Handles and retrieves the common or admin page depending on the URI template.
   * A user must be log-in first he can access these pages. Only the admin can see
   * the adminpage, however.
   */
  @Controller
  @RequestMapping(&quot;/main&quot;)
  public class MainController {
  
      protected static Logger logger = Logger.getLogger(&quot;controller&quot;);
  
      @Resource(name = &quot;adminService&quot;)
      private AdminService adminService;
  
      @Resource(name = &quot;userService&quot;)
      private UserService userService;
  
      /**
       * Handles and retrieves a /WEB-INF/jsp/employee-page.jsp
       *
       * containing all employee
       *
       * @return the name of the JSP page
       */
  
      @Secured(&quot;ROLE_USER&quot;)
      @RequestMapping(value = &quot;/employee-list&quot;,method = RequestMethod.GET)
      public String getEmployeeListPage(Model model) {
          logger.debug(&quot;Received request to show all employee page&quot;);
  
          // Retrieve all employee and attach to model
          model.addAttribute(&quot;all-employee&quot;, userService.findAllEmployee());
          return &quot;employee-page&quot;;
      }
  
      /**
       * Handles and retrieves /WEB-INF/jsp/admin-page.jsp that only admins can see
       *
       * @return the name of the JSP page
       */
      @Secured(&quot;ROLE_ADMIN&quot;)
      @RequestMapping(value = &quot;/admin&quot;, method = RequestMethod.GET)
      public String getAdminPage() {
          logger.debug(&quot;Received request to show admin page&quot;);
          return &quot;admin-page&quot;;
      }
  }
  </code></pre>
  <p>This is user service that had been called from controller:</p>
  <pre><code>package employee.service.impl;
  
  import employee.DAO.EmployeeInfoDAO;
  import employee.model.EmployeeInfo;
  import employee.service.UserService;
  import org.apache.log4j.Logger;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.access.annotation.Secured;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import java.util.List;
  
  /**
   * @author serge
   * 
   * Reader level
   * 
   * Service to Retrieve info about Employees
   */
  
  
  @Service(&quot;userService&quot;)
  @Transactional
  public class UserServiceImpl implements UserService {
      
      protected static Logger logger = Logger.getLogger(&quot;service&quot;);
      private EmployeeInfoDAO emplInfoDAO;
  
      @Autowired
      public void setEmployeeDao(EmployeeInfoDAO emplInfoDAO) {
          this.emplInfoDAO = emplInfoDAO;
      }
  
      /**
       * Retrieving Employee Information by id
       *
       * @return EmployeeInfo object
       */
      @Override
      @Transactional
      @Secured(&quot;ROLE_USER&quot;)
      public EmployeeInfo findEmployeeByID(Integer id) {
  
          logger.debug(&quot;Retrieving Employee with id= &quot; + id);
          EmployeeInfo employeeInfo = new EmployeeInfo();
          employeeInfo.setId(id);
          emplInfoDAO.find(employeeInfo);
          return employeeInfo;
      }
  
      /**
       * Retrieving all Employees  
       *
       * @return List of EmployeeInfo object
       */
      @Override
      @Secured(&quot;ROLE_USER&quot;)
      @Transactional(readOnly = true)
      public List&lt;EmployeeInfo&gt; findAllEmployee() {
          
          logger.debug(&quot;Retrieving all Employee&quot;);
          return emplInfoDAO.findAll();
      }
      
      /**
       * Retrieving Employees by last name
       *
       * @return List of EmployeeInfo object
       */
      @Override
      @Secured(&quot;ROLE_USER&quot;)
      @Transactional(readOnly = true)
      public List&lt;EmployeeInfo&gt; findAllByLastName(String lastName, Object paramValue) {
          lastName = &quot;lastName&quot;;
          
          logger.debug(&quot;Retrieving Employee by last name: &quot; + paramValue);
          return emplInfoDAO.findAllByParam(lastName, paramValue);
      }
      
      /**
       * Retrieving Employees by first name
       *
       * @return List of EmployeeInfo object
       */
      @Override
      @Secured(&quot;ROLE_USER&quot;)
      @Transactional(readOnly = true)
      public List&lt;EmployeeInfo&gt; findAllByFirstName(String firstName, Object paramValue) {
          firstName = &quot;firstName&quot;;
          
          logger.debug(&quot;Retrieving Employee by first name: &quot; + paramValue);
          return emplInfoDAO.findAllByParam(firstName, paramValue);
      }
  }
  </code></pre>
  <p>And this is custom DAO support, not sure if this is right way to do that.... :</p>
  <pre><code>package employee.DAO.impl;
  
  import employee.DAO.DAO;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.criterion.Restrictions;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.transaction.annotation.Transactional;
  
  import javax.annotation.Resource;
  import java.io.Serializable;
  import java.util.List;
  
  public class CustomHibernateDaoSupport&lt;T&gt;  implements DAO&lt;T&gt; {
      //was extending HibernateDaoSupport
      
      private Class&lt;T&gt; clazz;
      private SessionFactory sessionFactory;
  
      public CustomHibernateDaoSupport(Class&lt;T&gt; clazz) {
          this.clazz = clazz;
      }
  
      @Autowired
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  
  
      private Session getCurrentSession() {
          return sessionFactory.getCurrentSession();
      }
  
      @Override
      @Transactional
      public void save(T entity) {
          getCurrentSession().save(entity);
      }
  
      @Override
      @Transactional
      public void update(T entity) {
          getCurrentSession().update(entity);
      }
  
      @Override
      @Transactional
      public void delete(Serializable key) {
          Object entity = getCurrentSession().get(clazz, key);
          if (entity != null) {
              getCurrentSession().delete(entity);
          }
      }
  
      @Override
      @Transactional
      public T find(Serializable key) {
          return (T) getCurrentSession().get(clazz, key);
      }
  
      @Override
      @Transactional
      public List&lt;T&gt; findAll() {
          return getCurrentSession().createCriteria(clazz).list();
      }
  
      @Override
      @Transactional
      public List&lt;T&gt; findAllByParam(final String paramName, final Object paramValue) {
          return getCurrentSession().createCriteria(clazz)
                  .add(Restrictions.eq(paramName, paramValue))
                  .list();
      }
  }
  </code></pre>
  <p>And of course configuration:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; 
         xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
         xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
         xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-3.1.xsd
              http://www.springframework.org/schema/mvc 
              http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd&quot;&gt;
      
      &lt;!-- Activates annotations --&gt;
  
      &lt;context:annotation-config /&gt;
  
      &lt;mvc:annotation-driven/&gt;
  
      &lt;!-- Scans for annotated components in base-package--&gt;
  
      &lt;context:component-scan base-package=&quot;employee&quot; /&gt;
  
      &lt;bean class=&quot;employee.service.impl.AdminServiceImpl&quot;/&gt;
      &lt;bean class=&quot;employee.service.impl.UserServiceImpl&quot;/&gt;
  
      &lt;!-- Shared Hibernate SessionFactory in a Spring application context. --&gt;
  
      &lt;bean id = &quot;transactionManager&quot; class = &quot;org.springframework.orm.hibernate4.HibernateTransactionManager&quot;&gt;
          &lt;property name = &quot;sessionFactory&quot; ref = &quot;sessionFactory&quot; /&gt;
      &lt;/bean&gt;
  
      &lt;bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate4.LocalSessionFactoryBean&quot;&gt;
          &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/&gt;
  
          &lt;property name=&quot;annotatedClasses&quot;&gt;
              &lt;list&gt;
                  &lt;value&gt;employee.model.UserInfo&lt;/value&gt;
                  &lt;value&gt;employee.model.EmployeeInfo&lt;/value&gt;
                  &lt;value&gt;employee.model.EmployeeDiv&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
  
          &lt;property name=&quot;hibernateProperties&quot;&gt;
              &lt;props&gt;
                  &lt;prop key=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
                  &lt;prop key=&quot;hibernate.show_sql&quot;&gt;true&lt;/prop&gt;
                  &lt;prop key=&quot;hibernate.hbm2ddl.auto&quot;&gt;update&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- for database, imports the properties from database.properties --&gt;
  
      &lt;bean id=&quot;dataSource&quot; class=&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;&gt;
          &lt;property name=&quot;driverClassName&quot; value=&quot;${jdbc.driverClassName}&quot; /&gt;
          &lt;property name=&quot;url&quot; value=&quot;${jdbc.url}&quot; /&gt;
          &lt;property name=&quot;username&quot; value=&quot;${jdbc.username}&quot; /&gt;
          &lt;property name=&quot;password&quot; value=&quot;${jdbc.password}&quot; /&gt;
      &lt;/bean&gt;
  
      &lt;bean class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt;
          &lt;property name=&quot;location&quot; value=&quot;classpath:database.properties&quot;/&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  <p>after a successful login, I called /employee-list and got this instead of the page:</p>
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  ...
  
  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:941)
      employee.DAO.impl.CustomHibernateDaoSupport.getCurrentSession(CustomHibernateDaoSupport.java:31)
      employee.DAO.impl.CustomHibernateDaoSupport.findAll(CustomHibernateDaoSupport.java:64)
  ...
  </code></pre>
  <p><strong>Please help me understand where is my mistake, maybe I made a wrong configuration?</strong></p>