4

I am using version 2.2.0 of Spring Batch and version 3.6.4 of Hibernate. In my java-based spring configuration I want to configure my bean of the class HibernateItemWriter just like this:

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

But in this version of spring batch, the method setHibernateTemplate of the class HibernateItemWriter is deprecated.

/**
 * 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;
} 

I tried to configure my HibernateItemWriter as followed:

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

and I got this exception on the spring batch job execution:

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

Do you have any solution for that?

it seems like Spring Batch 2.2.0 does not support Hibernate 3

Thank you

4

2 回答 2

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);与休眠 3 兼容

于 2016-08-01T13:46:28.500 回答
0

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

于 2013-08-13T11:58:28.650 回答