You pass a thousand new entities to saveAll and the database sees a thousand separate inserts. How do you get them batched?
Spring Data JPA saveAll batching needs a Hibernate batch size, statements for the same table kept adjacent, and an identifier strategy that does not force a round trip per row. IDENTITY generation silently prevents insert batching, because Hibernate has to execute each insert to learn the key.
Answer
Short answer
Spring Data JPA saveAll is not a true bulk insert; it loops through entities and delegates to save. To make Hibernate batch the resulting inserts, configure hibernate.jdbc.batch_size, keep same-table statements adjacent with ordered inserts, use a sequence or pooled identifier strategy instead of IDENTITY, and flush/clear in chunks so the persistence context does not grow without bound.
Keep spring data jpa explicit in the answer because that is the concept the interviewer is actually trying to test. A good spring data jpa explanation names the trade-off, the failure mode, and the evidence you would use before choosing.
Was this answer helpful?
Likely follow-ups
- What does a pooled sequence optimiser do to the sequence of identifiers, and does that matter?
- When would you bypass JPA entirely for a bulk load, and what do you give up?
- Why does batching an update to a versioned entity need its own setting?
- How would you prove in an automated test that a save path batches?
Related questions
- Nobody called save, but the UPDATE still went to the database. Explain how that happened.mediumAlso on spring-data-jpa and jpa5 min
- A Spring Data repository method looks like one clean line, but the endpoint issues several hundred queries per request. How do you diagnose the N+1, and what would you actually change?hardAlso on spring-data-jpa and hibernate4 min
- This endpoint logs one query per row. Why are you seeing N+1 queries, and how do you fix them?mediumAlso on jpa and hibernate6 min
- Your lazy-loading proxy works until someone calls equals or getClass on it. What breaks, and how do you design a proxy that survives it?hardAlso on hibernate5 min