Spring 数据 JPA 的批量删除

Batch deletion for Spring data JPA

我有大约 20k 条记录要从 Spring 数据 JPA 查询中删除,查询与删除某个特定日期之前的所有记录有关。

我正在使用以下查询

dao.deleteByCreationDateBefore(new Date());

我认为此查询会在删除每一行时访问数据库。

请问有什么方法可以在这里使用批量删除吗?

此致

spting数据不支持批量操作

如果可能的话,尝试用简单的删除来完成,比如:它是非常快速的操作(比 butch delete 快)

从 SOME_ENTITY/TABLE 中删除,其中 CreationDate < new Date()/curentDate

如果您的 dao 方法逐条删除记录: 但是你可以在 dao level 中使用 hibernate/jpa 来做到这一点,比如来自 Hibernate/JPA Batch Insert and Batch Update Example:

    em.getTransaction().begin();
    for (int i = 0; i < 100; i++){
        Book book = new Book(i, "Hibernate/JPA Batch Insert Example: " + i);
        em.persist(book);

        if (i % batchSize == 0 && i > 0) {
            em.flush();
            em.clear();
        }
    }
    em.getTransaction().commit();

对于休眠: 这是文章 How to batch INSERT and UPDATE statements with Hibernate , read about 'Configuring hibernate.jdbc.batch_size'. And Hibernate JDBC and Connection Properties hibernate.jdbc.fetch_size 和 hibernate.jdbc.batch_size

的休眠选项