我正在使用Hibernate 5.4.0.Final 和Spring 4.3.13.RELEASE 我写了以下代码
@Bean
public SessionFactory sharedDataSessionFactory()
{
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(sharedDataSource());
builder.scanPackages("com.some.package", "scom.some.package1");
hibernateProperties.put("hibernate.dialect", MySQL5InnoDBDialect.class);
hibernateProperties.put("hibernate.id.new_generator_mappings", false);
hibernateProperties.put("hibernate.show_sql", false);
hibernateProperties.put("hibernate.cache.use_second_level_cache", false);
hibernateProperties.put("hibernate.cache.use_query_cache", false);
hibernateProperties.put("hibernate.jdbc.batch_size", 25);
builder.addProperties(hibernateProperties);
return builder.buildSessionFactory();
}
@Bean
public PlatformTransactionManager transactionManagerSharedData() {
return new HibernateTransactionManager(sharedDataSessionFactory());
}
但是当我运行测试代码时,它会像这样给出异常
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.transaction.PlatformTransactionManager]: Factory method 'transactionManagerSharedData' threw exception; nested exception is java.lang.ClassCastException: org.springframework.orm.hibernate5.LocalSessionFactoryBean$$EnhancerBySpringCGLIB$$d6b39340 cannot be cast to org.hibernate.SessionFactory
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 42 more
Caused by: java.lang.ClassCastException: org.springframework.orm.hibernate5.LocalSessionFactoryBean$$EnhancerBySpringCGLIB$$d6b39340 cannot be cast to org.hibernate.SessionFactory
at se.ss.some.backend.config.HibernateConfig$$EnhancerBySpringCGLIB$$2342b23c.sharedDataSessionFactory(<generated>)
at se.ss.some.backend.config.HibernateConfig.transactionManagerSharedData(HibernateConfig.java:106)
at se.ss.some.backend.config.HibernateConfig$$EnhancerBySpringCGLIB$$2342b23c.CGLIB$transactionManagerSharedData$5(<generated>)
at se.ss.some.backend.config.HibernateConfig$$EnhancerBySpringCGLIB$$2342b23c$$FastClassBySpringCGLIB$$7c29b43a.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
at se.mindspot.tender.backend.config.HibernateConfig$$EnhancerBySpringCGLIB$$2342b23c.transactionManagerSharedData(<generated>
)
但是我的应用程序运行正常。这个班的原因是什么?