【问题标题】:Hibernate Batch Insert not working in MySql [duplicate]休眠批量插入在MySql中不起作用[重复]
【发布时间】:2018-03-14 13:09:59
【问题描述】:

我正在尝试在春季批处理中使用休眠进行批量插入。我在这个站点上阅读了很多答案,然后尝试应用批量插入逻辑,但遇到了异常。我在下面发布了我的代码。请帮我解决这个问题。 下面是我的实体对象。

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,所以我还在我的连接 URL 中设置了属性“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”属性之前,我的代码工作正常,但批处理不起作用。现在插入属性后,我的代码抛出异常。请就此提供任何见解。

【问题讨论】:

  • 我不确定你如何准备items 收藏。如果您提供其代码,这可能有助于识别问题。
  • 我正在使用平面文件阅读器读取文件以读取每一行并将其映射到 VendorSource 的对象。然后在我的 customWriter 中,我收到了一个名为 item 的列表。每个对象都是 VendorSource 类型(为其提供源代码)。
  • 如果不使用"rewriteBatchedStatements=true",sql还会被批处理吗?
  • 可以分享一下文件吗?基本上我想看看你的对象是如何创建的以及在所有对象中设置了哪些字段。
  • 如果您使用的是 MySql,您必须将 rewriteBatchedStatements 设置为 true。这样驱动程序将能够检测到可以组合成单个插入语句的同一张表的插入

标签: java mysql hibernate


【解决方案1】:

问题是驱动程序由于某种原因没有提供正确的行数。将 MySql 连接器从 5.1.28 更新到 5.1.36 解决了这个问题。现在我可以看到正在批处理的语句。

【讨论】:

    猜你喜欢
    • 2015-09-08
    • 2020-09-26
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    相关资源
    最近更新 更多