2013-07-25 67 views
4

我使用Spring Batch的2.2.0版本和Hibernate的3.6.4版本。在我的基于java的spring配置我想配置我的类HibernateItemWriter绿豆就像这样:Spring批处理2.2.0和Hibernate 3兼容

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

但在这个版本Spring Batch的,该方法的类HibernateItemWritersetHibernateTemplate被弃用。

/** 
* 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不支持休眠3

谢谢

回答

0

只是一个尝试,如果你正在使用maven:使用排除标记从春季批次依赖排除hibernate.org包让Maven使用hibernate 3代构建您的应用程序。希望可以帮助

2

在发行说明中,截至Spring Batch 2.2.0;它似乎支持(默认情况下)休眠4.请参阅:引用org.springframework.batch.item.database.HibernateItemWriter

解决您的问题时BATCH-1904 Upgrade support of Hibernate to Hibernate 4

报告的错误会发生在运行时,用Hibernate 3(与3.6.2.RELEASE测试)时,使用自定义执行HibernateItemWriter。举例来看Spring 2.1.9(见HibernateItemWriter);这与Hibernate 3兼容