首页 > 解决方案 > JPA batch_size 属性不适用于本机查询

问题描述

我正在尝试使用本机查询进行批量插入。

@Repository
public interface Repository extends CrudRepository<Entity, Integer> {

    @Modifying
    @Query(value = "INSERT INTO table_name(value) VALUES (:value)", nativeQuery = true)
    void insert(@Param("value") String value);
}

我已将 batch_size 属性添加到 application.properties 文件

spring.jpa.properties.hibernate.jdbc.batch_size = 50

但是在日志中,我看到每个插入都是单独处理的。是否可以将批处理应用于本机查询?

标签: sqlspring-boothibernatejpabulk

解决方案


如果实体使用GenerationType.IDENTITY标识符生成器,​​休眠将静默禁用批量插入/更新。

详情请查看以下链接: https ://www.baeldung.com/jpa-hibernate-batch-insert-update#id-generation-strategy


推荐阅读