我在解决这个问题时遇到了困难:
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)
类:
的AppConfig
@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;
}
}
DataBaseConfig:
@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());
}
}
UserServices:
@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);
}
}
UserDaoImp
@Repository
public class UsuariosDaoImp implements UsuariosDao
{
@Autowired
private SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
public Usuarios findByUserName(String username)
{
List<Usuarios> users = new ArrayList<Usuarios>();
users = sessionFactory.getCurrentSession()
.createQuery("from usuarios where usuario=?")
.setParameter(0, username)
.list();
if (users.size() > 0)
{
return users.get(0);
}
else
{
return null;
}
}
}
我正在尝试了解Spring和Hibernate的配置。每次我尝试访问当前会话时,我都会在控制台中找到当前线程错误的无会话(不会停止应用程序)。 如果有人可以帮忙,我很感激!
答案 0 :(得分:2)
尝试将@EnableTransactionManagement
添加到DataBaseConfig