EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder

落花浮王杯 提交于 2019-12-22 18:37:41

问题


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

But I get exception:

java.lang.ClassCastException: 
org.springframework.orm.jpa.EntityManagerHolder cannot be cast to 
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 cannot be cast to org.springframework.orm.hibernate5.SessionHolder

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


回答1:


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.

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).

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.



来源:https://stackoverflow.com/questions/51294282/entitymanagerholder-cannot-be-cast-to-org-springframework-orm-hibernate5-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!