【发布时间】:2021-09-03 19:02:29
【问题描述】:
我想执行批量更新。我使用 JPA 和 Hibernate 5 作为 JPA 提供程序,并具有以下代码:
for (int i = 0; i < entities.size(); i++) {
if (i > 0 && i % JpaSettings.BATCH_SIZE == 0) {
entityManager.flush();
entityManager.clear();
}
....
count = count + entityManager.createQuery(criteria).executeUpdate();
}
entityManager.flush();
entityManager.clear();
但是,此代码似乎不执行批量更新。因为例如,当我插入时,我会在日志中看到如下内容:
DEBUG org.hibernate.engine.jdbc.batch.internal.BatchingBatch - Executing batch size: 2
但我在更新操作后没有看到此消息。谁能说如何使用 executeUpdate 进行批量更新?
【问题讨论】:
-
如果可能的话,我认为你应该找到一种方法在多个查询上执行更新。
-
您是否在代码中的某处更新了 i?条件查询有什么作用?
-
您期望什么样的批处理?通过批处理人们通常是指批处理插入,但您的示例似乎明确执行 DML 语句。 Hibernate 不能批处理 DML 语句,因为 API 总是必须返回更新计数,这需要立即执行语句。
-
您对 ID 使用哪种策略? stackoverflow.com/questions/63831206/…
-
批量大小参数似乎设置为 0,正如Documentation 所说,它依赖于 JDBC2 策略。