@BatchSize is a Hibernate annotation (org.hibernate.annotations.BatchSize) that makes lazy loading fetch several uninitialized collections or entity proxies with one SQL query instead of one query each. It is the simplest fix for the N+1 select problem when you cannot or do not want to use a JOIN FETCH. It is not part of the Jakarta Persistence specification, so it only works when Hibernate is your provider, which is the default in Spring Boot.
The number in size is how many parent collections (or entity ids) Hibernate initializes together in one query. It is not the number of child rows returned.
@BatchSize changes the query count@BatchSize, join fetch, and subselectSuppose a Department has a lazy @OneToMany collection of Employee rows. You load ten departments and then touch each employee list:
Hibernate runs one query for the departments, then one query per department the first time getEmployees() is touched. Ten departments produce eleven queries; a thousand produce 1,001. The cost grows with the number of parents, and every query is a network round trip.
@BatchSize changes the query countWith batch fetching enabled, when Hibernate needs to initialize one department's collection it also looks at other departments in the same persistence context whose collections are still uninitialized. It loads up to size of them in a single query that uses an IN list (or an array parameter on dialects that support it).
With ten departments and size = 20, the loop above needs two queries in total: one for the departments and one that loads the employees of all ten departments at once. With 100 departments it needs six (one plus five batches of 20).
Jakarta Persistence namespaces apply from Hibernate 6 and Spring Boot 3 onward (jakarta.persistence). On older stacks the same code uses javax.persistence; @BatchSize itself is unchanged.
@BatchSize can be placed in two spots, and they solve different problems.
| Placement | What it batches | Typical trigger |
|---|---|---|
| On a collection field | Initialization of that collection for several owners | Iterating parents and touching each child list |
| On the entity class | Loading of several entities (or proxies) of that type by id | A lazy @ManyToOne or getReference proxy being initialized |
Class-level example:
If 25 Order rows each point to a lazy Customer proxy, touching the first proxy lets Hibernate initialize up to 25 pending Customer proxies with one where id in (...) query instead of 25 separate selects.
Repeating the annotation on every association is tedious. Hibernate has a global default that applies to all lazy associations without an explicit @BatchSize:
In plain Hibernate the property is hibernate.default_batch_fetch_size. An explicit @BatchSize on a field or class overrides it. Many teams set a default between 16 and 100 and then tune only the hot paths; there is no universal best number, so measure with your own data.
@BatchSize, join fetch, and subselect| Approach | Queries | Strength | Weakness |
|---|---|---|---|
@BatchSize | 1 + ceil(N / size) | Works with pagination, no query rewrite | Still more than one query |
JOIN FETCH or @EntityGraph | 1 | Single round trip | Duplicates parent rows; collection fetch with setMaxResults paginates in memory |
@Fetch(FetchMode.SUBSELECT) | 2 | Loads all collections at once | Re-runs the original query as a subquery; can load far more than you need |
A common rule of thumb: use JOIN FETCH or an entity graph for a single known query, and use @BatchSize (or the global default) as a safety net for everything else. Batch fetching is especially valuable with paginated queries, because fetch-joining a collection while paginating makes Hibernate apply the limit in memory and log a warning.
EAGER association already loads in the original query or in follow-up selects, and a JOIN FETCH bypasses batching entirely.EntityManager or session are not grouped.IN lists. Some databases have limits on bind parameters, and plan caches work better with a small set of statement shapes.@BatchSize part of JPA?No. It lives in org.hibernate.annotations. A different JPA provider ignores it or fails to compile against it.
@BatchSize(size = 20) fetch 20 child rows?No. It initializes up to 20 parent collections (or 20 entities by id) per query. The query can return many child rows.
@BatchSize or JOIN FETCH?Use JOIN FETCH or an entity graph when one specific query always needs the association. Use @BatchSize when access patterns vary or you paginate.
Yes, because it runs on Hibernate. Annotate the entity fields or set spring.jpa.properties.hibernate.default_batch_fetch_size.
@BatchSize reduces N+1 queries by loading several lazy collections or entity proxies in one IN query. Place it on a collection or entity class, or set hibernate.default_batch_fetch_size for a global default. The size counts parents, not child rows. It is Hibernate-specific, helps most with paginated and varied access patterns, and complements rather than replaces JOIN FETCH and entity graphs.