The Wild

Hibernate Batch Insert Verification: How to Confirm

· 4 min read · Mammals

In this article
  1. Why Batch Inserts Matter
  2. Configuration Requirements for Batching
  3. How to Verify Batching: Step-by-Step
  4. Common Pitfalls That Disable Batching
  5. How to Choose the Right Batch Size
  6. Comparing Batching Across Databases
  7. Frequently Asked Questions

To verify that Hibernate actually performs batch inserts, enable SQL logging and check for the rewriteBatchedStatements=true parameter in your JDBC URL, then inspect the logs for multi-row insert statements. Alternatively, use a database-level query to count the number of round trips, or add a Hibernate statistics listener to monitor the getPrepareStatementCount() and getEntityInsertCount() values.

Why Batch Inserts Matter

Batch inserts reduce the number of database round trips by grouping multiple INSERT statements into a single network call. Without batching, inserting 10,000 entities would send 10,000 separate statements, each incurring network latency and database parsing overhead. With batching, those same 10,000 inserts might be sent in, say, 100 batches of 100, dramatically improving throughput. However, Hibernate does not batch inserts by default; you must explicitly configure it, and even then, certain conditions can silently disable batching.

Configuration Requirements for Batching

To enable batch inserts in Hibernate, you need to set the following properties in your hibernate.cfg.xml or application.properties:

  • hibernate.jdbc.batch_size – set to a positive integer (e.g., 30). This is the maximum number of statements Hibernate will group.
  • hibernate.order_inserts – set to true to order inserts by entity type, which helps Hibernate group them more effectively.
  • hibernate.order_updates – similarly, set to true for updates.
  • For MySQL and MariaDB, add rewriteBatchedStatements=true to the JDBC URL. Without this, the MySQL driver sends each statement individually despite batching.

Also, ensure your database driver supports batching. Most modern drivers do, but older ones may not.

How to Verify Batching: Step-by-Step

Here is a practical method to confirm that batching is happening:

  1. Enable SQL logging – Set hibernate.show_sql=true and hibernate.format_sql=true in your configuration. This will print every SQL statement to the console.
  2. Add the JDBC URL parameter – For MySQL, append rewriteBatchedStatements=true to your connection URL. For PostgreSQL, no extra parameter is needed; batching works out of the box.
  3. Run a test insert – Insert a few dozen entities in a loop using session.save() or entityManager.persist(). Flush and commit at the end.
  4. Inspect the logs – Look for a single insert statement that contains multiple value tuples. For example, insert into person (name) values ('John'), ('Jane'), ('Doe'). If you see separate insert statements for each row, batching is not working.
  5. Use Hibernate statistics – Enable hibernate.generate_statistics=true. After the operation, check the log output for statements executed and entity inserts. If the number of statements is significantly lower than the number of inserts, batching is active.

Common Pitfalls That Disable Batching

Even with configuration, batching may silently fail. Common causes include:

  • Identity strategy – If your entity uses GenerationType.IDENTITY for primary keys, Hibernate cannot batch inserts because it must retrieve the generated key immediately after each insert. Use SEQUENCE or TABLE strategies instead.
  • Mixing inserts with other operations – If you interleave inserts with updates or deletes, Hibernate may flush the batch prematurely. Use order_inserts and order_updates to mitigate this.
  • Flush mode – The default AUTO flush mode may cause Hibernate to flush before the batch is full. Consider setting hibernate.flush_mode=COMMIT or manually flushing at the end.
  • JDBC driver limitations – Some drivers do not support addBatch() properly. Check your driver documentation.

How to Choose the Right Batch Size

Batch size is a trade-off between memory and performance. A larger batch reduces round trips but consumes more memory and may lock database resources longer. A good starting point is 20–50, but benchmark with your specific workload and database. Monitor the time taken for inserts and adjust accordingly. Also, note that some databases have a maximum packet size; if a batch exceeds it, the driver may fail or split it.

Comparing Batching Across Databases

Batching behavior varies by database:

  • MySQL/MariaDB – Requires rewriteBatchedStatements=true to actually combine statements. Without it, the driver sends each insert individually.
  • PostgreSQL – Supports batching natively; the JDBC driver sends multi-row inserts automatically when batching is enabled.
  • Oracle – Supports batching but may require additional configuration like hibernate.jdbc.batch_versioned_data.
  • H2 – Supports batching, but you may need to set MODE=MySQL for compatibility.

Frequently Asked Questions

Why is my batch insert not working even with batch_size set?

Check if your entity uses IDENTITY generation; if so, Hibernate disables batching. Switch to SEQUENCE or TABLE. Also, verify that the JDBC URL includes rewriteBatchedStatements=true for MySQL. Finally, ensure you are not mixing inserts with other operations that trigger premature flushes.

How can I see the actual SQL being sent to the database?

Enable hibernate.show_sql=true and hibernate.format_sql=true. For more detailed logging, set the logger org.hibernate.SQL to DEBUG. This will show the exact statements, including whether they are batched.

Does batching work with Spring Data JPA?

Yes, Spring Data JPA uses Hibernate underneath, so the same configuration applies. Set spring.jpa.properties.hibernate.jdbc.batch_size and other properties in your application.properties. Ensure your repository methods are transactional and that you save entities in a loop within the transaction.

What is the difference between rewriteBatchedStatements and Hibernate's batch_size?

batch_size is a Hibernate-level setting that tells Hibernate how many statements to group. rewriteBatchedStatements is a MySQL JDBC driver setting that tells the driver to actually combine those grouped statements into a single multi-row insert. Without the driver setting, Hibernate may send the statements as a batch, but the driver sends them individually.

Can I verify batching without looking at logs?

Yes, you can use a database profiler like MySQL's general query log or PostgreSQL's pg_stat_statements to see the number of queries. Alternatively, enable Hibernate statistics and check the ratio of statements executed to entity inserts. If the ratio is close to 1, batching is not happening.