在spring boot hibernate中摆脱StaleStateException?

在spring boot hibernate中摆脱StaleStateException?,hibernate,session,exception,spring-boot,Hibernate,Session,Exception,Spring Boot,我已经通邮了。读了这篇文章后,我意识到我遇到了这个异常,因为对象在会话间持久化,在实际使用DAO删除对象之前,我必须先从会话中删除对象 如何在SpringBoot中获取会话,以便在使用DAO实际删除对象之前,首先从会话中删除对象 我已尝试将其添加到配置类: @Bean public HibernateJpaSessionFactoryBean sessionFactory() { return new HibernateJpaSessionFactoryBean(); } 并使用 @A

我已经通邮了。读了这篇文章后,我意识到我遇到了这个异常,因为对象在会话间持久化,在实际使用DAO删除对象之前,我必须先从会话中删除对象

如何在SpringBoot中获取会话,以便在使用DAO实际删除对象之前,首先从会话中删除对象

我已尝试将其添加到配置类:

@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
    return new HibernateJpaSessionFactoryBean();
}
并使用

@Autowired
SessionFactory sessionFactory;
最后使用会话:

Session session = sessionFactory.getCurrentSession();
session.delete(myEntity);
myDao.delete(myEntity);
但是我得到了这个错误:

org.hibernate.HibernateException: No CurrentSessionContext configured!
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:695)

在哪里配置数据库访问?这可能会对您有所帮助:尽管它使用JPA。您想直接访问Hibernate会话有什么原因吗?