休眠批处理

Hibernate Batch processing

所以我正在尝试配置 Hibernate 以进行批处理。我已经构建了一个示例应用程序并根据 Hibernates 文档进行了配置。

但是在配置 Hibernate 以注销 SQL 之后,看起来它根本没有执行批量插入,而只是执行单独的插入。我是不是读错了这个日志?

所以我在 Spring 引导应用程序中配置了以下属性。

spring.jpa.properties.hibernate.jdbc.batch_size=10
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true

这是我最基本的 Batch Writer..

@Transactional public class BatchWriter {</p> <pre><code>private EntityManager entityManager; private int batchSize; public BatchWriter(EntityManager entityManager, int batchSize) { this.entityManager = entityManager; this.batchSize = batchSize; } public void writeBatchOfCustomers(int numOf) { for(long i = 0; i <= numOf; i++) { Customer customer = new Customer(i); entityManager.persist(customer); if ( i % batchSize == 0 ) { //20, same as the JDBC batch size //flush a batch of inserts and release memory: entityManager.flush(); entityManager.clear(); } } }

}

现在我 运行 插入 20 个客户,例如,在休眠日志中我看到以下 20 次:

Hibernate: 
insert 
into
    customer
    (first_name, last_name, id) 
values
    (?, ?, ?)

我在这里错过了什么?

目前正在使用 Spring 引导自动配置 H2 数据库。然而,我最终会寻求将它与 Spring Batch 和 Oracle 数据库一起使用,这将插入大约 30k 个具有大约 35 个属性的对象。

感谢任何帮助。

谢谢,

因此看来休眠 SQL 日志记录相当具有误导性(在我看来)。

我的配置实际上是批处理。

我为此 class 添加了 DEBUG 级别的记录器:

org.hibernate.engine.jdbc.batch.internal.BatchingBatch

当前在 Hibernate 版本 5.0.12 中命名。 (认为​​它以前被命名为其他名称)。

在这个class你可以看到它实际上是批处理。