Spring批次2.2.0和Hibernate 3兼容

时间:2013-07-25 13:00:14

标签: java spring hibernate spring-batch

我使用的是Spring Batch的2.2.0版本和Hibernate的3.6.4版本。在我的基于java的spring配置中,我想配置类HibernateItemWriter的bean,就像这样:

@Bean
public <E> ItemWriter<E> hibernateItemWriter(HibernateOperations hibernateTemplate) {
    HibernateItemWriter<E> writer = new HibernateItemWriter<E>();
    writer.setHibernateTemplate(hibernateTemplate);
    return writer;
}

但是在此版本的spring批处理中,不推荐使用类setHibernateTemplate的方法HibernateItemWriter

/**
 * Public setter for the {@link HibernateOperations} property.
 *
 * @param hibernateTemplate
 *            the hibernateTemplate to set
 * @deprecated As of 2.2 in favor of using Hibernate's session management APIs directly
 */
public void setHibernateTemplate(HibernateOperations hibernateTemplate) {
    this.hibernateTemplate = hibernateTemplate;
} 

我尝试按照以下方式配置HibernateItemWriter

@Bean
public <E> ItemWriter<E> hibernateItemWriter(SessionFactory sessionFactory) {
    HibernateItemWriter<E> writer = new HibernateItemWriter<E>();
    writer.setSessionFactory(sessionFactory);
    return writer;
}

我在弹出批处理作业执行时得到了这个例外:

java.lang.NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()Lorg/hibernate/Session;
at org.springframework.batch.item.database.HibernateItemWriter.doWrite(HibernateItemWriter.java:135)
at org.springframework.batch.item.database.HibernateItemWriter.write(HibernateItemWriter.java:113)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:175)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:151)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.write(SimpleChunkProcessor.java:274)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:199)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:395)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:267)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:77)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:253)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:195)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:137)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:152)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:131)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:135)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:301)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:134)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:48)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:127)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:117)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at sun.proxy.$Proxy20.run(Unknown Source)
at org.springframework.batch.test.JobLauncherTestUtils.launchJob(JobLauncherTestUtils.java:152)
at ...

你有解决方法吗?

看起来Spring Batch 2.2.0不支持Hibernate 3

谢谢

2 个答案:

答案 0 :(得分:2)

在发行说明中,截至Spring Batch 2.2.0;它似乎支持(默认情况下)Hibernate 4.参见:BATCH-1904 Upgrade support of Hibernate to Hibernate 4

在引用Hibernate 3

时使用3.6.2.RELEASE(使用org.springframework.batch.item.database.HibernateItemWriter测试)时,报告的错误将在运行时发生

要解决您的问题,请使用HibernateItemWriter的自定义实现。作为示例,请参阅Spring 2.1.9中此类的来源(请参阅HibernateItemWriter);这与Hibernate 3兼容

答案 1 :(得分:0)

试一试如果您正在使用maven:使用排除标记从spring-batch依赖项中排除hibernate.org包,让maven使用hibernate 3 deps构建您的应用程序。希望可以帮助