Sub topic 14
JDBC Batching for Inserts and Updates
This chapter assumes you know Spring Data JPA repositories, basic entity mapping, and how the persistence context tracks entities. N+1 is a read problem. Writes have a similar cost: each insert or update normally becomes its own statement and its own network round trip. JDBC batching groups many statements into one round trip.
Why writes are slow by default
Suppose you save 1,000 Book rows with saveAll. Hibernate queues 1,000 inserts in the persistence context. At flush time, it sends them to the database one at a time unless you enable batching. The database work is small, but 1,000 round trips add up, especially over a network.
With a batch size of 50, Hibernate sends the same 1,000 inserts in about 20 batches.
Enabling batching
Add these properties to application.properties:
spring.jpa.properties.hibernate.jdbc.batch_size=50
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=truejdbc.batch_sizesets the maximum number of statements per batch.order_insertssorts pending inserts by entity type. Without it, inserts that alternate betweenAuthorandBooksplit the batch every time the table changes.order_updatesdoes the same for updates.
The IDENTITY restriction
Insert batching does not work with GenerationType.IDENTITY. With IDENTITY, the database assigns the id, so Hibernate must run each insert immediately to learn the id. That makes batching impossible for inserts.
The example model from earlier chapters uses IDENTITY. To batch inserts, switch to a sequence:
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "book_seq")
@SequenceGenerator(name = "book_seq", sequenceName = "book_seq", allocationSize = 50)
private Long id;
private String title;
@ManyToOne(fetch = FetchType.LAZY)
private Author author;
}Set allocationSize equal to the batch size. Hibernate then reserves 50 ids per sequence call instead of asking the database for each id.
Checking that it works
The SQL log does not show batches. Hibernate logs each statement as if it were sent alone. Use the statistics from the earlier chapter instead. With hibernate.generate_statistics=true, Hibernate logs a session summary that includes the number of JDBC batches executed. You should see a small number of batches, not one per row.
Common mistakes
- Using IDENTITY and expecting batching. The setting is silently ignored for inserts.
- Saving inside a loop with separate transactions. Each transaction flushes on its own, so batches stay tiny. Wrap the work in one
@Transactionalmethod. - Never clearing the persistence context. Importing 100,000 entities in one transaction keeps all of them in memory. Call
flush()andclear()on theEntityManagerevery batch-size entities. - Expecting a speedup from a few rows. Batching matters for tens or hundreds of rows, not three.
Updates batch without an id-strategy restriction. Hibernate groups the dirty-checked updates at flush time, and order_updates keeps same-table updates together.
Exercise
You set hibernate.jdbc.batch_size=50 and save 500 Book entities, but the statistics show about 500 JDBC statements and no batching. The Book id uses GenerationType.IDENTITY. What is the cause, and what change fixes it?
Show answer
With IDENTITY the database generates the id, so Hibernate must execute each insert immediately to read the id back, which disables insert batching. Change the id to GenerationType.SEQUENCE with a @SequenceGenerator whose allocationSize matches the batch size (50), and migrate the database sequence to match.