Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

How do you enable JDBC batching for inserts and updates in Hibernate with Spring Boot, and why might inserts still not be batched?

We set spring.jpa.properties.hibernate.jdbc.batch_size (for example 50) and usually hibernate.order_inserts and hibernate.order_updates so statements for the same table are grouped together. Inserts can still go one by one when the entity uses IDENTITY id generation, because Hibernate must run each insert immediately to learn the generated id; SEQUENCE ids with a pooled optimizer fix that. The driver matters too: MySQL only sends real multi-row inserts with rewriteBatchedStatements=true.

Why this question is asked

Batching is one of the cheapest performance wins in JPA, and also one of the most commonly misconfigured. A team sets the batch size, sees no improvement, and concludes batching does not work, when in fact one small detail such as the id strategy switched it off silently. Interviewers ask this because the full answer shows we understand both what Hibernate does at flush time and what the JDBC driver does underneath. In this answer, we will recap what JDBC batching is, enable it, see why ordering matters, look at the id generation trap, and finish with the driver settings and how to verify that batching really happens.

1A Quick Recap of JDBC Batching

Without batching, every insert or update is a separate round trip: the application sends one statement, the database runs it and replies, and only then does the next statement go. With a 1 ms network round trip, 10,000 inserts cost at least 10 seconds of waiting alone.

JDBC batching lets us add many executions of the same prepared statement to a batch with addBatch() and send them together with executeBatch(). The statement text is the same; only the parameter values differ. Hibernate does this for us at flush time, the moment it writes pending changes to the database, if we tell it how many statements to group.

2Enabling Batching in Spring Boot

Hibernate settings are passed through spring.jpa.properties. The key setting is the batch size. A value between 20 and 100 is typical; beyond that the gain is small and memory use grows.

Java
spring.jpa.properties.hibernate.jdbc.batch_size=50
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
// 1,000 new Order entities persisted, then flushed:
// without batch_size: 1,000 round trips
// with batch_size=50: 20 round trips of 50 inserts each

Notice that batch_size only controls how many statements are grouped. It does not change when Hibernate flushes. The statements are still collected in the persistence context until a flush, which happens at commit, before a query that might need the pending data, or when we call flush() ourselves.

3Why Ordering Inserts and Updates Matters

A JDBC batch can only contain executions of one statement. If our loop saves an Order and then its OrderLine, then the next Order and its line, the statement text alternates: insert into orders, insert into order_line, insert into orders. Each change of statement closes the current batch, so we end up with batches of size one.

hibernate.order_inserts=true tells Hibernate to sort the pending inserts by entity type before executing them, while still respecting foreign keys. All orders go first in batches of 50, then all lines. order_updates does the same for updates, sorting by entity type and id, which also makes lock order predictable and reduces deadlocks between concurrent transactions.

The same flush with and without ordering
SettingStatement sequenceBatches sent
order_inserts=falseorders, line, orders, line, ...200 batches of size 1 for 100 orders
order_inserts=true100 x orders, then 100 x line4 batches of 50

4The IDENTITY Trap

Here is the most common reason inserts are still not batched. With GenerationType.IDENTITY, the database assigns the id when the row is inserted, for example with an auto-increment column. Hibernate needs that id at persist() time, because the entity must be stored in the persistence context under its id. The only way to get it is to run the insert immediately and read the generated key back. So Hibernate silently disables insert batching for IDENTITY entities; updates and deletes can still be batched.

Java
@Entity
class Order {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
Long id; // each persist() runs its insert at once: no insert batching
}
@Entity
class Order {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "order_seq")
@SequenceGenerator(name = "order_seq", sequenceName = "order_seq", allocationSize = 50)
Long id; // ids come from the sequence in blocks; inserts wait for the flush
}

With SEQUENCE, Hibernate fetches ids from a database sequence before the insert, so the insert itself can wait for the flush and join a batch. The pooled optimizer, which Hibernate uses when allocationSize is greater than 1, makes this cheap: one call to the sequence reserves 50 ids, so 10,000 entities need 200 sequence calls instead of 10,000. The database sequence must be created with INCREMENT BY 50 to match allocationSize, otherwise ids clash or Hibernate refuses to start, depending on the schema validation setting.

5The Driver Has a Say Too

Even when Hibernate calls executeBatch(), the driver decides what goes over the wire. MySQL Connector/J by default still sends each statement of the batch separately; with rewriteBatchedStatements=true it rewrites a batch of inserts into one multi-row INSERT ... VALUES (...), (...), which is much faster. The PostgreSQL driver pipelines batched statements well already and has a similar reWriteBatchedInserts=true option for multi-row inserts.

Properties
spring.datasource.url=jdbc:mysql://db:3306/shop?rewriteBatchedStatements=true
spring.datasource.url=jdbc:postgresql://db:5432/shop?reWriteBatchedInserts=true

6Checking That Batching Really Happens

We should never assume. Hibernate statistics (hibernate.generate_statistics=true) report the number of JDBC batches executed, and a JDBC-level proxy such as datasource-proxy logs each batch with its size. If the log shows batch size 1 for every insert, one of the causes above is at work: IDENTITY ids, mixed statement types without ordering, or a flush after every save.

Java
spring.jpa.properties.hibernate.generate_statistics=true
// Session metrics at the end of the transaction:
// 20 JDBC batches executed -> batching works
// 1000 JDBC statements executed, 0 batches -> it does not

7Follow-up questions

Does Spring Data's saveAll() batch automatically?

saveAll() just calls save() for each entity in one transaction. Whether the inserts are batched depends on the same settings: batch_size, the id strategy and ordering. Also, if the entities have assigned ids, save() calls merge(), which first runs a select per entity, so batching the inserts does not remove those extra round trips.

Can versioned entities be batched for updates?

Yes, in current Hibernate versions with modern drivers. Hibernate checks the update counts returned by the batch to detect optimistic lock failures. Older versions had a hibernate.jdbc.batch_versioned_data setting that was sometimes needed; check your version.

Why not use a huge batch size like 10,000?

Hibernate keeps every pending entity in the persistence context until flush, so memory grows with the batch, and the database must parse and hold large requests. Gains flatten after a few dozen statements per batch, while the cost keeps growing. Values of 20 to 100 work well, combined with flush and clear in chunks for large imports.

8Conclusion

In this answer, we enabled JDBC batching with hibernate.jdbc.batch_size and made it effective with order_inserts and order_updates, which group statements by table. We saw that IDENTITY ids disable insert batching because Hibernate must insert immediately to get each id, and that SEQUENCE ids with a pooled optimizer and matching allocationSize solve it. We also saw that the driver must cooperate, with rewriteBatchedStatements=true on MySQL, and that Hibernate statistics or a datasource proxy tell us whether batches really go out.

What to say in the interviewAbout 70 seconds aloud

Open with

I enable it with spring.jpa.properties.hibernate.jdbc.batch_size, usually around 50, and I also set hibernate.order_inserts and order_updates to true. Batching only groups executions of the same statement, so if a flush alternates between orders and order lines, every batch has size one; ordering sorts pending statements by entity type so they group properly.

Then, if they want more

The most common reason inserts are still not batched is IDENTITY id generation. Hibernate needs the id at persist time, and with an auto-increment column the only way to get it is to run the insert immediately, so it disables insert batching for that entity.

The fix is SEQUENCE generation with the pooled optimizer, an allocationSize of say 50 that matches the sequence's increment, so ids are reserved in blocks and inserts can wait for the flush. Then the driver: on MySQL I add rewriteBatchedStatements=true so the batch becomes a multi-row insert, and PostgreSQL has reWriteBatchedInserts.

Finally I verify with Hibernate statistics or datasource-proxy that batches of 50 are actually executed, rather than assuming it works.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do you enable JDBC batching for inserts and updates in Hibernate with Spring Boot...": https://scrivio-interview-library.netlify.app/databases/jpa-performance/4fc65d7b2c/