Spring DAO rewritebatchedstatement在带有EmbeddedId

时间:2019-05-21 18:21:58

标签: mysql hibernate spring-boot spring-data-jpa

我们需要在 mysql 上快速插入500万条记录(此实例无控制权)。

尝试了所有以下内容,但无济于事:

  1. 设置rewritebatchedstatements=true(通过url和props)
  2. 尝试了useServerPrepStmts=trueuseServerPrepStmts=true
  3. 尝试了以下其他冬眠道具
  4. StoryGenerationType.Auto,所以还可以,但是TaskEmbeddedId,所以批处理应该可以吗? (因为未指定任何世代)
  5. 尝试过MySql5InnoDBDialectMySql5DBDialect

基于统计数据的批量处理有效,但rewritebatchedstatements无效。

spring.jpa.properties.hibernate.jdbc.batch_size=500
spring.jpa.properties.hibernate.jdbc.fetch_size=500
spring.jpa.properties.hibernate.jdbc.order_inserts=500
spring.jpa.properties.hibernate.jdbc.order_updates=500

spring.jpa.properties.hibernate.batch_versioned_data=true
spring.jpa.properties.hibernate.generate_statistics=true

spring.datasource.hikari.data-source-properties.rewriteBatchedStatements=true
#spring.datasource.hikari.data-source-properties.useServerPrepStmts=true
spring.datasource.hikari.data-source-properties.useServerPrepStmts=false

示例实体:

class SprintService {

   @Transactional
    public void startSprint(Epic epic, List<Stories> stories) {
        epic.save()
        .....
        stories.saveAll() // few hundreds of records
        Set<Task> tasks = new HashSet<>;
        for(Story story: stories)
        {
            tasks.addAll(story.getTasks());
            tasks.addAll(story.getTasks());
            tasks.addAll(story.getTasks());
            //do something
        }
        tasks.saveAll() // few million records
    }
}

@Table(name = "STORY")
@EqualsAndHashCode
class Story {
   @Id
   @GeneratedValue(stratergy=GenerationType.Auto)
   @Column(name="STORY_ID")
   Long id;

   @Column(name="STORY_NAME")
   String name;

   @OneToMany(cascade=MERGE, mappedBy="story", fetch = FetchType.Lazy)
   Set<Task> task;
}



@Entity
@Table(name = "TASK_XREF")
class Task {
    @EmbeddedId
    TaskPKId taskPKId;

    @Column(name = "TASK_NAME")
    String name;
    @MapsId("storyId")
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "STORY_ID")
    Story story;

    //getters, setters

}

这是TaskPKID类:

@Embeddable
class TaskPKId  implements Serializable {
        long taskId;
        long taskTypeId;
        @Column(name="STORY_ID")
        long storyId;

    public long getTaskId() {
        return taskId;
    }

    public void setTaskId(long taskId) {
        this.taskId = taskId;
    }

    public void setTaskTypeId(long taskTypeId) {
        this.taskTypeId = taskTypeId;
    }
}

1 个答案:

答案 0 :(得分:1)

值应为布尔值。

spring.jpa.properties.hibernate.jdbc.order_inserts=true
spring.jpa.properties.hibernate.jdbc.order_updates=true