gpt4 book ai didi

java - Spring + hibernate : No Session Found For Current Thread

转载 作者:行者123 更新时间:2023-11-30 08:55:00 24 4
gpt4 key购买 nike

我在解决这个问题时遇到困难:

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)

类:

应用配置

@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;
}
}

数据库配置:

@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());
}
}

用户服务:

@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 的配置。每次我尝试访问当前 session 时,我都会在我的控制台中收到“找不到当前线程的 session ”错误(不会停止应用程序)。如果有人可以提供帮助,我将不胜感激!

最佳答案

尝试将 @EnableTransactionManagement 添加到 DataBaseConfig

关于java - Spring + hibernate : No Session Found For Current Thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29244926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com