'Spring Boot JPA/JDBC batching findById works but findOneByX not working

I am using Spring Boot JPA, I have enabled batching by ensuring the following lines are in the my application.properties:

spring.jpa.properties.hibernate.jdbc.batch_size=1000
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true

I now have a loop where I am doing a findById on an entity and then saving that entity like so:

var entity = dao.findById(id)
// Do some stuff
dao.save(entity) //This line is not really required but I am being explicit here

Putting the above in a loop I see that the save(update) statements are batched to the DB. My issue is that if I do a findOneByX where X is a property on the entity then the batching does not work (batch size of 1), requests get sent one at a time i.e.:

var entity = dao.findOneByX(x)
// Do some stuff
dao.save(entity)

Why is this happening? Is JPA/JDBC only equipped to batch when we findById only?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source