hibernate batch_size配置不执行多行插入查询

时间:2018-06-28 08:33:09

标签: java hibernate spring-data-jpa

我有以下使用hibernate 5.2.17.Final的实体以及Spring Data JPA,oracle 11g;

@Getter
@Setter
@Entity
@Table(name = "cats")
public class CatEntity {

    @EmbeddedId
    private CompositeKey compositeKey;

    @JsonBackReference
    @MapsId("key")
    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "P_ID", referencedColumnName = "P_ID", nullable = false)
    private ParentEntity parent;

    @JsonManagedReference
    @OneToOne(mappedBy = "cat", cascade = ALL, orphanRemoval = true)
    private CatDetailEntity detail;

    @JsonManagedReference
    @OneToMany(mappedBy = "cat", cascade = ALL, orphanRemoval = true)
    private Set<KittenEntity> kittenSet;

    @JsonManagedReference
    @OneToMany(mappedBy = "cat", cascade = ALL, orphanRemoval = true)
    private Set<MateEntity> mateSet;

    @Data
    @Embeddable
    public static class CompositeKey implements Serializable {

        @MapsId
        @Column(name = "P_ID")
        private String key;

        @Column(name = "REG_DATE")
        private LocalDateTime date;
    }
}

&使用此表的端点一次插入数百个Cat。因此,我从Hibernate获得了以下日志;

Hibernate: insert into cats(P_ID, REG_DATE) values (?, ?)
Hibernate: insert into cats(P_ID, REG_DATE) values (?, ?)
Hibernate: insert into kittens(P_ID, REG_DATE, foo, bar) values (?, ?, ?, ?)
Hibernate: insert into kittens(P_ID, REG_DATE, foo, bar) values (?, ?, ?, ?)
...

但是我想利用batch_size,所以对于这4个表,我最多可以有4个插入查询。基本上是CatKittenMateDetail。 因此,请执行以下配置;

spring:
  jpa:
    properties:
      hibernate:
        order_inserts: true
        order_updates: true
        jpa:
          batch_size: 50
          batch_versioned_data: true

它可用于插入和更新的排序,但是batch_size从未按预期使用,我希望为每个表进行单个INSERT ALL查询。由于基础设施的要求,我担心的是要尽可能减少查询。

具有多行插入的预期插入查询;

INSERT ALL
  INTO cats(P_ID, REG_DATE) values (1, foo)
  INTO cats(P_ID, REG_DATE) values (2, bar)
SELECT * FROM dual
INSERT ALL
  INTO kittens(P_ID, REG_DATE, foo, bar) values (1, foo, fooz, foox)
  INTO kittens(P_ID, REG_DATE, foo, bar) values (2, bar, barz, barx)
SELECT * FROM dual

0 个答案:

没有答案