spring-data-jpa Sping Boot JPA批量插入

wnvonmuf  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(319)

我有3个实体父,子,子。父是子的父,子是子的父。我需要插入大约700个父对象。父可以有50个子对象。子可以有50个子对象。我尝试了正常的repository.save(ListOfObjects),大约需要4分钟。
然后我尝试使用实体管理器的persistflushclear基于批处理大小(500)。这也花了大约4分钟。在性能上没有太大的区别。请建议一个最好的方法来有效地插入这么大的数据量。
父项

@Entity
public class Parent {
@Id @GeneratedValue(strategy= GenerationType.AUTO)
private Long parentId;
private String aaa;
private String bbb;
private String ccc;
@Version
private Long version;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "parent", fetch = FetchType.LAZY)
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(column=@JoinColumn(name="parentId",referencedColumnName="parentId",nullable=false))})
private List<Child> childs = new ArrayList<>();
public Long getParentId() {
    return parentId;
}
public void setParentId(Long parentId) {
    this.parentId = parentId;
}
public String getAaa() {
    return aaa;
}
public void setAaa(String aaa) {
    this.aaa = aaa;
}
public String getBbb() {
    return bbb;
}
public void setBbb(String bbb) {
    this.bbb = bbb;
}
public String getCcc() {
    return ccc;
}
public void setCcc(String ccc) {
    this.ccc = ccc;
}
public Long getVersion() {
    return version;
}
public void setVersion(Long version) {
    this.version = version;
}
public List<Child> getChilds() {
    return childs;
}
public void setChilds(List<Child> childs) {
    this.childs = childs;
}
}

子项

@Entity
public class Child {
@Id @GeneratedValue(strategy= GenerationType.AUTO)
private Long childId;
private String ddd;
private String ccc;
private Integer eee;
@OneToMany(cascade = CascadeType.ALL,orphanRemoval = true, mappedBy = "child", fetch = FetchType.LAZY)
@JoinColumnsOrFormulas({
        @JoinColumnOrFormula(column = @JoinColumn(name = "childId", referencedColumnName = "childId", nullable = false)) })
private List<SubChild> subChilds = new ArrayList<>();
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumnsOrFormulas({
    @JoinColumnOrFormula(column= @JoinColumn( name="parentId",referencedColumnName="parentId",nullable=false))
})
private Parent parent;

public Long getChildId() {
    return childId;
}
public void setChildId(Long childId) {
    this.childId = childId;
}
public String getDdd() {
    return ddd;
}
public void setDdd(String ddd) {
    this.ddd = ddd;
}
public String getCcc() {
    return ccc;
}
public void setCcc(String ccc) {
    this.ccc = ccc;
}
public Integer getEee() {
    return eee;
}
public void setEee(Integer eee) {
    this.eee = eee;
}
public List<SubChild> getSubChilds() {
    return subChilds;
}
public void setSubChilds(List<SubChild> subChilds) {
    this.subChilds = subChilds;
}
public Parent getParent() {
    return parent;
}
public void setParent(Parent parent) {
    this.parent = parent;
}
}

子项

@Entity
public class SubChild {
@Id @GeneratedValue(strategy= GenerationType.AUTO)
private Long subChildId;
private String fff;
private String ggg;
private Integer hhh;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumnsOrFormulas({
    @JoinColumnOrFormula(column= @JoinColumn( name="childId",referencedColumnName="childId",nullable=false))
})
private Child child;

public Long getSubChildId() {
    return subChildId;
}
public void setSubChildId(Long subChildId) {
    this.subChildId = subChildId;
}
public String getFff() {
    return fff;
}
public void setFff(String fff) {
    this.fff = fff;
}
public String getGgg() {
    return ggg;
}
public void setGgg(String ggg) {
    this.ggg = ggg;
}
public Integer getHhh() {
    return hhh;
}
public void setHhh(Integer hhh) {
    this.hhh = hhh;
}
public Child getChild() {
    return child;
}
public void setChild(Child child) {
    this.child = child;
}
}

用于保持父实体列表的资料档案库方法

@Value("${spring.jpa.hibernate.jdbc.batch_size}")
private int batchSize;

public <T extends Parent> Collection<T> bulkSave(Collection<T> entities) {
    final List<T> savedEntities = new ArrayList<T>(entities.size());
    int i = 0;
    for (T t : entities) {
        savedEntities.add(persistOrMerge(t));
        i++;
        if (i % batchSize == 0) {
            // Flush a batch of inserts and release memory.
            entityManager.flush();
            entityManager.clear();
        }
    }
    return savedEntities;
}
private <T extends Parent> T persistOrMerge(T t) {
    if (t.getTimeSlotId() == null) {
        entityManager.persist(t);
        return t;
    } else {
        return entityManager.merge(t);
    }
}

application.yml

spring:
  application:
    name: sample-service
  jpa:
    database: MYSQL
    show-sql: true
    hibernate:
      ddl-auto: update
      dialect: org.hibernate.dialect.MySQL5Dialect
      naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
      jdbc:
        batch_size: 100
  jackson:
    date-format: dd/MM/yyyy
  thymeleaf:
    cache: false
spring.datasource.url : jdbc:mysql://${dbhost}/sample?createDatabaseIfNotExist=true
spring.datasource.username : root
spring.datasource.password : root
spring.datasource.driver-class-name : com.mysql.cj.jdbc.Driver
0vvn1miw

0vvn1miw1#

要启用批量插入,您需要配置中的batch_size属性。
此外,由于jdbc批处理只能以一个表为目标,因此需要使用spring.jpa.hibernate.order_inserts=true属性来对父表和子表之间的插入进行排序,否则语句将是无序的,您将看到一个部分批处理(在调用不同表中的插入时,将出现新批处理)

相关问题