EntityManagerHolder无法转换为org.springframework.orm.hibernate5.SessionHolder [英] EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder

查看:670
本文介绍了EntityManagerHolder无法转换为org.springframework.orm.hibernate5.SessionHolder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码通过Hibernate配置Spring:

I'm using this code to configure Spring with Hibernate:

@SpringBootApplication
@Configuration
@EnableTransactionManagement
public class ContextServer {

    @Bean
    public LocalSessionFactoryBean getSessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

        try {
            sessionFactory.setDataSource(dataSource());
        } catch (NamingException e) {
            e.printStackTrace();
        }
        sessionFactory.setPackagesToScan(new String[] { "org.plugin.database.models" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        // factoryBean.setAnnotatedClasses(User.class, Authorities.class);

        return sessionFactory;
    }

    @Bean
    public DataSource dataSource() throws NamingException {
        return (DataSource) new JndiTemplate().lookup("java:/global/production_gateway");
    }

    private final Properties hibernateProperties() {
        final Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
        hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MariaDBDialect");
        hibernateProperties.setProperty("hibernate.show_sql", "true");
        hibernateProperties.setProperty("hibernate.format_sql", "true");

        return hibernateProperties;
    }

    @Bean
    public HibernateTransactionManager getTransactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(getSessionFactory().getObject());
        return transactionManager;
    }
}

我以这种方式使用工厂:

I use the factory this way:

@Component("authorize")
@Transactional
public class AuthorizeService implements MessageProcessor {

    @Autowired
    SessionFactory sessionFactory;


    @Override
    public void processMessage(.....) {     

        Session session = sessionFactory.getCurrentSession();
    }

但我得到了例外:

java.lang.ClassCastException: 
org.springframework.orm.jpa.EntityManagerHolder cannot be cast to 
org.springframework.orm.hibernate5.SessionHolder

我找到了以下答案: ClassCastException:org.springframework.orm.jpa.EntityManagerHolder无法强制转换到org.springframework.orm.hibernate5.SessionHolder

I found these answers: ClassCastException: org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder

Spring 4 + Hibernate 5 = org.springframework .orm.jpa.EntityManagerHolder无法转换为org.springframework.orm.hibernate5.SessionHolder

还有其他可以使用的解决方案吗?上面的解决方案是快速的技巧.

Is there any other solution that I can use? Above solutions are quick hacks.

推荐答案

随着Hibernate 5.2的发布,SessionFactory扩展了EntityManagerFactory接口.这导致SessionFactory也是EntityManagerFactory.

With the release of Hibernate 5.2 the SessionFactory extends the EntityManagerFactory interface. This leads to the SessionFactory also being an EntityManagerFactory.

在以前的休眠版本中并非如此.

In previous hibernate releases this wasn't the case.

简单的解决方案是将休眠版本降级为< 5.2,因为Spring 5.0没有解决方案(Spring 5.1中将提供解决方案).

The easy solution is to downgrade the hibernate version to a version < 5.2 as there is no solution for Spring 5.0 (there will be in Spring 5.1).

或者甚至更好的是不要使用普通的SessionFactory并让Spring Boot自动配置EntityManagerFactory(如果检测到休眠,默认情况下完成),并使用它代替普通的Hibernate.

Or probably even beter don't use the plain SessionFactory and let Spring Boot autoconfigure the EntityManagerFactory (done by default if hibernate is detected) and use that instead of plain Hibernate.

这篇关于EntityManagerHolder无法转换为org.springframework.orm.hibernate5.SessionHolder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆