【发布时间】:2017-09-13 18:22:58
【问题描述】:
所以我正在尝试构建一个使用 Hibernate 写入 MySQL 数据库的应用程序,并且有一段时间试图让它工作。简而言之,它没有看到会话,即使我已经配置并自动装配了一个。这是我的代码明智的。
首先是该应用的配置:
@EnableBatchProcessing
@SpringBootApplication(scanBasePackages="com.lcbo")
@EnableIntegration
public class BatchConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job processLCBOInventory(@Qualifier("getCurrentLCBODataStep") final Step getCurrentLCBODataStep,
@Qualifier("getLCBOStoreDataStep") final Step getLCBOStoreDataStep,
final JobExecutionListenerConfig jelcListener
) {
return jobBuilderFactory
.get("processLCBOInventory")
.incrementer(new RunIdIncrementer())
.start(getCurrentLCBODataStep)
.next(getLCBOStoreDataStep)
.listener(jelcListener)
.build();
}
@Bean
public Step getLCBOStoreDataStep(final LCBOStoreReader lcboStoreReader,
final LCBOStoreProcessor lcboStoreProcessor,
final LCBOStoreWriter lcboStoreWriter,
final ExecutionContextPromotionListener listener) throws Exception {
return stepBuilderFactory
.get("getLCBOStoreDataStep")
.<LCBOStore, LCBOStore>chunk(inventoryTrackerProperties.getDefaults().getChunkSize())
.reader(lcboStoreReader.read())
.processor(lcboStoreProcessor)
.writer(lcboStoreWriter)
.listener(listener)
.build();
}
那么,作者:
@Component
public class LCBOStoreWriter extends HibernateItemWriter<LCBOStore> {
@Autowired
private LCBOStoreService lcboStoreService;
@Autowired
private DatabaseConfig dbConfigy;
private static final Logger log = LoggerFactory.getLogger(LCBOStoreWriter.class);
@Override
public void write(List<? extends LCBOStore> lcboStoreItems) {
for (LCBOStore lcboStoreItem : lcboStoreItems) {
lcboStoreService.addNewStore(lcboStoreItem);
}
}
以及 DatabaseConfig 文件内容:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.lcbo.domain")
public class DatabaseConfig {
@Autowired
private LCBOInventoryTrackerProperties properties;
@Bean
public EntityManagerFactory entityManagerFactory() throws SQLException {
final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource());
factoryBean.setJpaDialect(new HibernateJpaDialect());
factoryBean.setJpaProperties(getHibernateProperties());
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factoryBean.setPackagesToScan("com.lcbo.domain");
factoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
factoryBean.setPersistenceUnitName("persistenceUnit");
factoryBean.afterPropertiesSet();
return factoryBean.getObject();
}
@Bean(destroyMethod = "")
public DataSource dataSource() throws SQLException {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(properties.getDb().getDriver());
dataSource.setUrl(properties.getDb().getUrl());
dataSource.setUsername(properties.getDb().getUsername());
dataSource.setPassword(properties.getDb().getPassword());
return dataSource;
}
甚至尝试过 SessionFactory 本身,像这样
public LocalSessionFactoryBean getSessionFactory() throws SQLException {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.bytestree.model" });
sessionFactory.setHibernateProperties(getHibernateProperties());
return sessionFactory;
}
但没有骰子。我还尝试将 SessionFactory 实例注入 HibernateItemWriter 附带的 setSessionFactory 中,同样没有骰子。
我知道我有什么问题,问题是它期望 SessionFactory 被放置在哪里以被应用程序重新配置,或者我是否已将这个编写器配置在任何接近正确的地方?
编辑:根据要求,运行此代码时在 intellj 中显示的堆栈跟踪。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'LCBOInventoryWriter' defined in file [C:\workspace\LCBOInventoryTracker\target\classes\com\lcbo\writer\LCBOInventoryWriter.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Either HibernateOperations or SessionFactory must be provided
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at com.lcbo.config.LCBOBatchConfig.main(LCBOBatchConfig.java:157) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_102]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_102]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_102]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]
Caused by: java.lang.IllegalStateException: Either HibernateOperations or SessionFactory must be provided
at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.batch.item.database.HibernateItemWriter.afterPropertiesSet(HibernateItemWriter.java:93) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.6.RELEASE.jar:4.3.6.RELEASE]
... 20 common frames omitted
【问题讨论】:
-
作者的
SessionFactory设置在哪里? -
你能显示完整的堆栈跟踪/错误吗?你如何设置春季批次?我注意到在您的
LCBOStoreWriter.write()中,您将 Session 转换为 SessionFactory。那是行不通的!尝试使用SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);设置 HibernateItemWriter 的 SessionFactory -
HibernateItemWriter需要在afterPropertiesSet方法中检查的明确连接的sessionFactory。另外恕我直言,您的应用程序存在缺陷,因为您不需要SessionFactory和EntityMangerFactory。你为什么还要使用HibernateItemWriter?您应该改用JpaItemWriter,因为您不需要现在的破解/解决方法。 -
@MichaelMinella 我在编写器中使用 this.setSessionFactory(databaseConfig.getSessionFactory()) 设置 SessionFactory 的地方无效。
-
只需使用一个普通的
JpaItemWriter你不需要扩展它,这是你的全部问题,你的扩展正在破坏作者的正常工作。
标签: java mysql hibernate spring-boot spring-batch