0

I want to execute batch update. I use JPA and Hibernate 5 as JPA provider and have the following code:

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();

However, this code seems not to execute batch updates. Because when I, for example, do insert I see in log something like:

DEBUG org.hibernate.engine.jdbc.batch.internal.BatchingBatch - Executing batch size: 2

But I don't see this message after my update operations. Could anyone say how to do batch update using executeUpdate?.

ThatsMe
  • 123
  • 4
  • 24
  • I think you should find a way to executeUpdate on multiple queries, if it is possible. – Pavel_K Jun 19 '21 at 19:25
  • Do you update i somewhere in your code? What does the criteriaquery do? – Kirinya Jun 21 '21 at 14:51
  • What kind of batching do you expect? By batching people usually refer to batching up inserts, but your sample seems to explicitly execute DML statements. Hibernate can't batch DML statements as the API always must return the update count which requires the statement to be executed immediately. – Christian Beikov Jun 21 '21 at 15:36
  • Which strategy do you use for ID? https://stackoverflow.com/questions/63831206/spring-boot-appllication-batch-doesnt-work-in-jparepository-saveall-method/63836771#63836771 – yazabara Jun 25 '21 at 18:29
  • Batch size parameter seems set to 0 which falls back on JDBC2 strategy as [Documentation](https://docs.jboss.org/hibernate/orm/5.5/userguide/html_single/Hibernate_User_Guide.html#batch-jdbcbatch) says. – Constantin Konstantinidis Jun 26 '21 at 09:40

3 Answers3

0

You might just see data in that session if you do not have autocommit enabled.

entityManager.flush()

Sends data to the DB and it's persisted for your session, but within transactional context. So if the transaction is not committed or rolled back or client is killed data of the transaction will not be transactionally be persisted to the table and hence you'll not be able to access it.

So try calling

entityManager.getTransaction().commit()

and see if this changes behaviour. Note the transaction then can't be rolled back any more, but that should be obvious.

supernova
  • 1,762
  • 1
  • 14
  • 31
0

This is not possible to do using .executeUpdate() because when you call this method hibernate will execute this sql statement to get count of modified rows. So, there is no way to do batch update with .executeUpdate().

Pavel_K
  • 10,748
  • 13
  • 73
  • 186
-1

This can be achieved using Session operations through EniityManager - persist or setting the updated value for a persistent instance of the entity class.

for (int i = 0; i < entities.size(); i++) {
    if (i > 0 && i % JpaSettings.BATCH_SIZE == 0) {
        entityManager.flush();
        entityManager.clear();
    }
    ....
    entityManager.persist(obj);
}
entityManager.flush();
entityManager.clear();

As for the number of records updated, you could check the query executed in the DB logs.

One thing to take care of while executing batch updates is the properties - hibernate.order_updates and hibernate.batch_versioned_data need to be set to true. The blog below demonstrates it.

Blog : https://www.baeldung.com/jpa-hibernate-batch-insert-update