hibernate bulk update with batching

247 views Asked by At

I have a code which updates a bunch or records of a table using hibernate bulk update feature (update table set=:arg where id =:id ...), in a for loop. So, if there are 100 records to be updated then 100 times the update is fired from hibernate. Is there a way to package these multiple updates into a single batch update (read jdbc batch) (or at least reduce the updates fired)? I understand hibernate performs batching implicitly but in the above case it does not seem to be happening.

1

There are 1 answers

0
Vlad Mihalcea On

Hibernate tries to batch DML operations during flush time, so make user you set the following properties:

<property name="hibernate.order_inserts" value="true"/>
<property name="hibernate.order_updates" value="true"/>
<property name="hibernate.jdbc.fetch_size" value="20"/>
<property name="hibernate.jdbc.batch_size" value="50"/>

Also take into consideration that an IDENTITY generator will disable JDBC batching.