我正在尝试使用春季批处理中的hibernate进行批量插入。我在这个网站上阅读了很多答案,然后尝试应用批量插入逻辑,但遇到了异常。我在下面发布了我的代码。请帮我解决这个问题。 下面是我的实体对象。
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Immutable;
@Entity
@Table(name = "vendor_source")
@Immutable
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region = "vendorSourceRegion")
public class VendorSource implements java.io.Serializable {
private static final long serialVersionUID = 7529960788000388791L;
private String sourceCode;
private String sourceName;
private Date createTime = new Date();
private Date updateTime = new Date();
private String createId = " ";
private String updateId = " ";
public VendorSource() {
}
public VendorSource(String sourceCode, String sourceName, String filler, Date createTime, Date updateTime,
String createId, String updateId) {
super();
this.sourceCode = sourceCode;
this.sourceName = sourceName;
this.createTime = createTime;
this.updateTime = updateTime;
this.createId = createId;
this.updateId = updateId;
}
@Id
@Column(name = "source_code", unique = true, nullable = false, length = 1)
public String getSourceCode() {
return sourceCode;
}
public void setSourceCode(String sourceCode) {
this.sourceCode = sourceCode;
}
@Column(name = "source_name", nullable = false, length = 35)
public String getSourceName() {
return sourceName;
}
public void setSourceName(String sourceName) {
this.sourceName = sourceName;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "create_time", nullable = false, length = 19)
public Date getCreateTime() {
return this.createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "update_time", nullable = false, length = 19)
public Date getUpdateTime() {
return this.updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
@Column(name = "create_id", nullable = false, length = 254)
public String getCreateId() {
return this.createId;
}
public void setCreateId(String createId) {
this.createId = createId;
}
@Column(name = "update_id", nullable = false, length = 254)
public String getUpdateId() {
return this.updateId;
}
public void setUpdateId(String updateId) {
this.updateId = updateId;
}
}
我还在会话工厂中设置了这些属性
hibernate.jdbc.batch_size=20
hibernate.jdbc.batch_versioned_data=true
hibernate.order_inserts=true
hibernate.order_updates=true
这是我的作家代码:
Session session = spinSessionFactory.openSession();
Transaction tx=session.beginTransaction();
long prev = Calendar.getInstance().getTimeInMillis();
for(VendorSource item:items){
i++;
session.save(item);
if(i%20==0){
session.flush();
session.clear();
}
}
tx.commit();
session.close();
我正在使用MySQl所以我也设置了属性" rewriteBatchedStatements = true"在我的连接网址中。 运行代码时出现以下异常:
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into vendor_source (create_id, create_time, source_name, update_id, update_time, source_code) values (?, ?, ?, ?, ?, ?)
2017-10-03 16:15:45.571 ERROR [BatchingBatch] - HHH000315: Exception executing batch [Batch update returned unexpected row count from update [0]; actual row count: 20; expected: 1]
2017-10-03 16:15:45.584 ERROR [AbstractStep] - Encountered an error executing the step
org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 20; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:89)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73)
at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.checkRowCounts(BatchingBatch.java:151)
at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:128)
at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.addToBatch(BatchingBatch.java:97)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at com.searshc.hs.item.master.update.vendor.charges.writer.CustomWriter.write(CustomWriter.java:36)
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:133)
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:50)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:127)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:351)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:577)
在我把" rewriteBatchedStatements = true"之前属性,我的代码工作正常但批处理不起作用。现在插入属性后我的代码抛出异常。请提供任何见解。
答案 0 :(得分:0)
问题是驱动程序由于某种原因没有给出正确的行数。将MySql连接器从5.1.28更新到5.1.36解决了这个问题。现在我可以看到批量陈述。