For the N+1 queries issue in Hibernate, I add @BatchSize(size=20) on collection and class level. And it works very well as it fetches 20 records each time.
Now I found that there is a hibernate.jdbc.fetch_size configuration option which I think it is the same as @BatchSize annotation.
However, it has no effect to the N+1 queries issue when I configured it in my project.
Am I wrong with hibernate.jdbc.fetch_size ?
What is hibernate.jdbc.fetch_size for?
The
hibernate.jdbc.fetch_sizeis a JDBC driver configuration, and it determines:Hibernate uses this configuration to set the PreparedStatement.fetchSize and according to JDBC documentation:
So this setting is only a hint and it allows the JDBC Driver to retrieve the selected rows in batches, to minimize the number of database network round-trips.
The
@BatchSizeis a Hibernate fetching optimization and it controls the number of SELECT statements being generated.