使用ibatis将数据插入弹簧批处理中的多个表中

时间:2014-08-09 17:58:36

标签: spring-batch ibatis

我正在使用IbatisBatchItemWriter将复杂对象写入多个表。

以下是我的目标

public class SfObject{
  protected List<Person> person;  
}
public class Person {
  protected String personId;
  protected XMLGregorianCalendar dateOfBirth;
  protected String countryOfBirth;
  protected String regionOfBirth;
  protected String placeOfBirth;
  protected String birthName;
  protected XMLGregorianCalendar dateOfDeath;
  protected XMLGregorianCalendar lastModifiedOn;
  protected List<EmailInformation> emailInformation;
}
public class EmailInformation {
  protected String emailType;
  protected String emailAddress;
  protected XMLGregorianCalendar lastModifiedOn;
}

这是我的ibatis配置,以插入上述对象

<insert id="insertCompoundEmployeeData" parameterClass="com.domain.SfObject">
 <iterate property="person">
  insert into E_Person_Info
   (person_id,
   person_birth_dt,
   person_country_of_birth,
   person_region_of_birth,
   person_place_of_birth,
   person_birth_name,
   person_death_dt,
   last_modified_on
   )        
  values (#person[].personId#,
   #person[].dateOfBirth,
   #person[].countryOfBirth#,
   #person[].regionOfBirth#,
   #person[].placeOfBirth#,
   #person[].birthName#,
   #person[].dateOfDeath#,
   #person[].lastModifiedOn#
  );
  <iterate property="person[].emailInformation">
   insert into E_Email_Info
    (email_info_person_id,
    email_info_email_type,
    email_info_email_address,
    last_modified_on
   )        
   values (#person[].personId#,
    #person[].emailInformation[].emailType#,
    #person[].emailInformation[].emailAddress#,
    #person[].emailInformation[].lastModifiedOn#
   );                       
  </iterate>                                                
 </iterate>       
</insert>

我不确定我是否可以使用上面的配置将数据插入到多个表中,但是当我执行上面的代码时,我的批次为10条记录时出现以下错误。顺便说一句,电子邮件信息不是强制性的,因此在某些人物对象中可能为空。

Stacktrace
[08.08.2014 17:30:07] DEBUG: WebservicePagingItemReader.doRead() - Reading page 0
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=1
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=2
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=3
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=4
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=5
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=6
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=7
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=8
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.executeInternal() - Repeat operation about to start at count=9
[08.08.2014 17:30:09] DEBUG: RepeatTemplate.isComplete() - Repeat is complete according to policy and result value.
[08.08.2014 17:30:09] DEBUG: IbatisBatchItemWriter.write() - Executing batch with 10 items.
[08.08.2014 17:30:09] DEBUG: SqlMapClientTemplate.execute() - Opened SqlMapSession [com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl@168afdd] for iBATIS operation
[08.08.2014 17:30:10] DEBUG: Connection.debug() - {conn-100000} Connection
[08.08.2014 17:30:10] DEBUG: SqlMapClientTemplate.execute() - Obtained JDBC Connection [Transaction-aware proxy for target Connection  from DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@8eae04]] for iBATIS operation
[08.08.2014 17:30:10] DEBUG: Connection.debug() - {conn-100000} Preparing Statement:        insert into E_Person_Info        (person_id,        person_birth_dt,        person_country_of_birth,        person_region_of_birth,        person_place_of_birth,        person_birth_name,        person_death_dt,        last_modified_on        )         values (?,         ?,         ?,         ?,         ?,         ?,         ?,         ?         );                               
[08.08.2014 17:30:10] DEBUG: Connection.debug() - {conn-100000} Preparing Statement:        insert into E_Person_Info        (person_id,        person_birth_dt,        person_country_of_birth,        person_region_of_birth,        person_place_of_birth,        person_birth_name,        person_death_dt,        last_modified_on        )         values (?,         ?,         ?,         ?,         ?,         ?,         ?,         ?         );          insert into E_Email_Info          (email_info_person_id,          email_info_email_type,          email_info_email_address,          last_modified_on          )           values (?,           ?,           ?,           ?           );                                        
[08.08.2014 17:30:10] DEBUG: Connection.debug() - {conn-100000} Preparing Statement:        insert into E_Person_Info        (person_id,        person_birth_dt,        person_country_of_birth,        person_region_of_birth,        person_place_of_birth,        person_birth_name,        person_death_dt,        last_modified_on        )         values (?,         ?,         ?,         ?,         ?,         ?,         ?,         ?         );                               
[08.08.2014 17:30:10] DEBUG: TaskletStep.doInChunkContext() - Applying contribution: [StepContribution: read=10, written=0, filtered=0, readSkips=0, writeSkips=0, processSkips=0, exitStatus=EXECUTING]
[08.08.2014 17:30:10] DEBUG: TaskletStep.doInChunkContext() - Rollback for Exception: org.springframework.dao.InvalidDataAccessResourceUsageException: Batch execution returned invalid results. Expected 1 but number of BatchResult objects returned was 3
[08.08.2014 17:30:10] DEBUG: DataSourceTransactionManager.processRollback() - Initiating transaction rollback
[08.08.2014 17:30:10] DEBUG: DataSourceTransactionManager.doRollback() - Rolling back JDBC transaction on Connection [net.sourceforge.jtds.jdbc.ConnectionJDBC3@190d8e1]
[08.08.2014 17:30:10] DEBUG: DataSourceTransactionManager.doCleanupAfterCompletion() - Releasing JDBC Connection [net.sourceforge.jtds.jdbc.ConnectionJDBC3@190d8e1] after transaction
[08.08.2014 17:30:10] DEBUG: DataSourceUtils.doReleaseConnection() - Returning JDBC Connection to DataSource
[08.08.2014 17:30:10] DEBUG: RepeatTemplate.doHandle() - Handling exception: org.springframework.dao.InvalidDataAccessResourceUsageException, caused by: org.springframework.dao.InvalidDataAccessResourceUsageException: Batch execution returned invalid results. Expected 1 but number of BatchResult objects returned was 3
[08.08.2014 17:30:10] DEBUG: RepeatTemplate.executeInternal() - Handling fatal exception explicitly (rethrowing first of 1): org.springframework.dao.InvalidDataAccessResourceUsageException: Batch execution returned invalid results. Expected 1 but number of BatchResult objects returned was 3
[08.08.2014 17:30:10] ERROR: AbstractStep.execute() - Encountered an error executing the step
org.springframework.dao.InvalidDataAccessResourceUsageException: Batch execution returned invalid results. Expected 1 but number of BatchResult objects returned was 3
    at org.springframework.batch.item.database.IbatisBatchItemWriter.write(IbatisBatchItemWriter.java:140)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:156)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:137)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.write(SimpleChunkProcessor.java:252)
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:178)
    at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:74)
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:268)
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:76)
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:367)
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:143)
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:242)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:198)
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:348)
    at org.springframework.batch.core.job.flow.FlowJob.access$0(FlowJob.java:1)
    at org.springframework.batch.core.job.flow.FlowJob$JobFlowExecutor.executeStep(FlowJob.java:135)
    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:144)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:124)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:103)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:250)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:110)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:48)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:105)
    at com.CtrlMPojoForBatch.initiateSpringBatchProcess(CtrlMPojoForBatch.java:92)
    at com.CtrlMPojoForBatch.main(CtrlMPojoForBatch.java:33)
[08.08.2014 17:30:10] WARN: CustomStepExecutionListner.afterStep() - Failure occured executing the step readWriteExchagnerateConversionData
[08.08.2014 17:30:10] WARN: CustomStepExecutionListner.afterStep() - Initiating the rollback operation... 
[08.08.2014 17:30:10] WARN: CustomStepExecutionListner.afterStep() - Rollback completed!

1 个答案:

答案 0 :(得分:2)

假设您正在使用Spring Batch中提供的IbatisBatchItemWriter(它已被弃用而不支持MyBatis项目提供的那些),请将assertUpdates设置为false。这将阻止Spring Batch验证每个项目只进行了一次更新。

相关问题