title: Spring + Hibernate : No Session Found For Current Thread
body: <p>I'm having difficulties solving this problem:</p>
  
  <pre><code>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:993)
      at com.veram.dao.UsuariosDaoImp.findByUserName(UsuariosDaoImp.java:23)
      at com.veram.servicos.ServicosUsuario.loadUserByUsername(ServicosUsuario.java:31)
      at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:101)
      at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:132)
      at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
      at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:177)
      at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
      at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:211)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  </code></pre>
  
  <p>Classes:</p>
  
  <p>AppConfig</p>
  
  <pre><code>@EnableWebMvc
  @Configuration
  @ComponentScan(basePackages = { "com.ver.*"})
  @Import({ SecurityConfig.class, DataBaseConfig.class })
  public class AppConfig extends WebMvcConfigurerAdapter 
  {       
      //Adiciona a pasta resources ao dispatcher do MVC
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) 
      {
          registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
      }
  
      @Override
      public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) 
      {
          configurer.enable();
      }
  
      //Localização das views da minha aplicação
      @Bean
      public InternalResourceViewResolver viewResolver() 
      {
          InternalResourceViewResolver viewResolver 
                            = new InternalResourceViewResolver();
  
          viewResolver.setViewClass(JstlView.class);
          viewResolver.setPrefix("/WEB-INF/views/");
          viewResolver.setSuffix(".jsp");
          return viewResolver;
      }       
  }
  </code></pre>
  
  <p>DataBaseConfig:</p>
  
  <pre><code>@Configuration
  @Import({ SecurityConfig.class })
  public class DataBaseConfig 
  {       
      @Bean
      public SessionFactory sessionFactory() 
      {
              LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource());
  
              builder.scanPackages("com.ver.entid").addProperties(getHibernateProperties());
              return builder.buildSessionFactory();
      }
  
      private Properties getHibernateProperties() 
      {
          Properties prop = new Properties();
  
          prop.put("hibernate.format_sql", "true");
          prop.put("hibernate.show_sql", "true");
          prop.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
          return prop;
      }
  
      @Bean(name = "dataSource")
      public BasicDataSource dataSource() 
      {
          BasicDataSource ds = new BasicDataSource();
              ds.setDriverClassName("com.mysql.jdbc.Driver");
  
          ds.setUrl("jdbc:mysql://localhost:3306/veram_prod");
          ds.setUsername("root");
          ds.setPassword("root");
          return ds;          
      }
  
      @Bean
      public HibernateTransactionManager txManager() 
      {
          return new HibernateTransactionManager(sessionFactory());
      }     
  }
  </code></pre>
  
  <p>UserServices:</p>
  
  <pre><code>@Service("userDetailsService")
  public class ServicosUsuario implements UserDetailsService 
  {   
      @Autowired
      private UsuariosDao userDao;
  
      @Transactional(readOnly=true)
      @Override
      public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException 
      {           
          com.veram.entidades.Usuarios user = userDao.findByUserName(username);
      }
  }
  </code></pre>
  
  <p>UserDaoImp</p>
  
  <pre><code>@Repository
  public class UsuariosDaoImp implements UsuariosDao
  {       
      @Autowired
      private SessionFactory sessionFactory;
  
      @SuppressWarnings("unchecked")
      public Usuarios findByUserName(String username)
      {     
          List&lt;Usuarios&gt; users = new ArrayList&lt;Usuarios&gt;();
  
          users = sessionFactory.getCurrentSession()
              .createQuery("from usuarios where usuario=?")
              .setParameter(0, username)
              .list();
  
          if (users.size() &gt; 0) 
          {
              return users.get(0);
          } 
          else 
          {
              return null;
          }     
      }    
  }
  </code></pre>
  
  <p>I'm trying to understand the configuration for Spring and Hibernate. Every time I try to access the current session, I get a no session found for current thread error in my console (doesn't stop the app).
  If anyone can help, I appreciate!</p>