Quick revision
The short answer
Batch fetching loads uninitialized proxies or collections for several owners at once with an IN list of ids; @BatchSize sets the batch size for one entity or association, and hibernate.default_batch_fetch_size sets a global default. FetchMode.SUBSELECT applies to collections only and loads them for all owners returned by the original query, using a subquery that repeats that query. None of them needs query changes, but all still run more than one query, unlike a fetch join.
What to remember
In this answer, we saw that batch fetching loads lazy collections or proxies for several owners with an IN list, configured per mapping with @BatchSize or globally with default_batch_fetch_size, and that SUBSELECT loads a collection for every owner of the original query with a subquery. All three avoid query changes but still run more than one query, while a fetch join loads everything in one statement for a specific use case.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Batch fetching loads uninitialized proxies or collections for several owners at once with an IN list of ids; @BatchSize sets the batch size for one entity or association, and hibernate.default_batch_fetch_size sets a global default. FetchMode.SUBSELECT applies to collections only and loads them for all owners returned by the original query, using a subquery that repeats that query. None of them needs query changes, but all still run more than one query, unlike a fetch join.
Why this question is asked
Fetch joins are the precise tool against N+1, but they must be written for each use case, and large codebases have hundreds of use cases. Batch fetching and subselect fetching are the broad tools: they change how lazy loading itself works, so many N+1 patterns shrink without anyone editing a query. Interviewers ask how they differ because they are often confused, and choosing the wrong one can load far more data than expected. In this answer, we will recap the N+1 pattern, see how batch fetching works with @BatchSize and the global setting, see how SUBSELECT works, and compare all of them with a fetch join.
1A Quick Recap of the Problem
Suppose a query returns 100 authors and each author has a lazy books collection. Hibernate puts a wrapper in each author's books field, which loads its own elements the first time it is used. If our code loops over the authors and reads author.getBooks().size(), each wrapper loads separately: select ... from book where author_id = ? runs 100 times. The same happens with lazy @ManyToOne proxies, such as book.getPublisher(). That is the N+1 problem: 1 query for the owners, N for their associations.
Each of the three settings changes what happens at the moment the first wrapper or proxy is initialized. Instead of loading just that one, Hibernate loads several, or all.
2Batch Fetching: Several Owners per Query
With batch fetching, when one uninitialized collection or proxy must be loaded, Hibernate looks in the persistence context, the session's map of loaded entities, for other uninitialized collections or proxies of the same kind, and loads up to the batch size of them in one query with an IN list of their ids.
- Code touches authors[0].booksuninitialized wrapper
- Find other uninitialized books collectionsin the persistence context
- Take up to batch size idsfor example 25
- Run one IN querywhere author_id in (...)
- Fill all those wrappersnext 24 accesses need no query
5 steps in order. Play them, or step through them one at a time.
So 101 queries become 5. Notice the two placements. On a collection mapping, @BatchSize controls how many of that collection are loaded together. On an entity class, it controls how many proxies of that entity are initialized together, which helps every @ManyToOne pointing to it. The global setting, hibernate.default_batch_fetch_size, applies a default size to every entity and collection that has no @BatchSize, which is why many teams set it once to a value like 16 to 50 as a safety net.
The exact SQL varies. Older Hibernate versions padded IN lists to a few fixed sizes so statements could be cached. Hibernate 6 changed how batch keys are sent, and on databases with array support such as PostgreSQL it can bind all ids as one array parameter, as in where author_id = any(?). Check your version, and the SQL log, to see what your database receives.
3SUBSELECT: All Owners From the Original Query
FetchMode.SUBSELECT is a Hibernate-specific setting for collections only. When the first collection is initialized, Hibernate loads the collection for every owner that the original query returned, in one query, by putting the original query inside a subquery.
Two queries in total, however many authors there are. Hibernate does not need to list the ids; it re-runs the filtering part of the original query as a subquery. There is a catch: if the original query was expensive, the database evaluates it again. And if the original query returned 10,000 authors but we only look at the books of the first one, SUBSELECT still loads books for all 10,000, because it cannot know we will stop. Batch fetching is more cautious, loading only up to the batch size at a time.
4Comparing Them With a Fetch Join
All three settings share one property that makes them attractive: they are configured on the mapping or globally, so existing queries benefit without changes. They also share a limit: the data still arrives in more than one query. A fetch join, select a from Author a left join fetch a.books, loads owners and collections in one statement, at the cost of writing it for each use case and, for collections, multiplying rows and preventing SQL pagination.
| Approach | Scope | Queries | Watch out for |
|---|---|---|---|
@BatchSize(25) | one entity or association | 1 + 4 | size choice per mapping |
| default_batch_fetch_size=25 | all lazy associations | 1 + 4 | global, can hide hot paths |
FetchMode.SUBSELECT | collections only | 1 + 1 | re-runs the query, loads for all owners |
| left join fetch | one query | 1 | row multiplication, no SQL paging |
5Choosing Between Them
My default is a global default_batch_fetch_size of about 16 to 50, because it caps the damage of every N+1 we have not found yet, and the larger IN lists are cheap on modern databases. For hot paths I still write explicit fetch joins, entity graphs or DTO queries, so the most important screens run the minimum number of queries and their behaviour does not depend on what happens to be in the persistence context.
I use SUBSELECT rarely, for collections that are almost always read for every owner of a moderate result set, such as tags on a page of 50 articles. And I always check the SQL log after changing any of them, since the result depends on the query, the session state and the Hibernate version.
6Follow-up questions
Does batch fetching help with lazy @ManyToOne proxies too?
Yes. With @BatchSize on the target entity class, or with the global setting, initializing one Customer proxy initializes up to the batch size of other Customer proxies in the session with one IN query. That is the most common N+1 in practice, and the global setting handles it with no code changes.
Why can batch fetching run more queries than expected?
Because it only batches what is already uninitialized in the session. If the code loads owners in several separate queries, or initializes associations at different times, the batches are smaller. The IN list also depends on the batch size, so 1,000 owners with a size of 25 still means 40 extra queries.
Can SUBSELECT be used with pagination?
It can, but the subquery repeats the original query, and how the database handles limit and offset inside a subquery depends on the database and version. Check the generated SQL to be sure the collections match the page you loaded.
7Conclusion
In this answer, we saw that batch fetching loads lazy collections or proxies for several owners with an IN list, configured per mapping with @BatchSize or globally with default_batch_fetch_size, and that SUBSELECT loads a collection for every owner of the original query with a subquery. All three avoid query changes but still run more than one query, while a fetch join loads everything in one statement for a specific use case.
Open with
All three change what Hibernate does when a lazy collection or proxy is first initialized. With batch fetching, Hibernate looks in the persistence context for other uninitialized collections or proxies of the same kind and loads up to the batch size of them in one query with an IN list of owner ids, so 100 authors with a batch size of 25 cost 1 plus 4 queries instead of 1 plus 100. @BatchSize sets that size for one association, or on an entity class for its proxies, and hibernate.default_batch_fetch_size sets a global default for everything without its own annotation.
Then, if they want more
FetchMode.SUBSELECT is for collections only: on first access, Hibernate loads the collection for all owners returned by the original query, by repeating that query as a subquery, so it is always two queries, but it re-runs the query and loads collections for every owner even if we only read one.
None of them requires changing queries, which makes the global batch size a good safety net, but they all still run more than one query, unlike a fetch join or entity graph, which I still write for hot paths.
Hibernate 6 changed how batch keys are sent, for example array parameters on PostgreSQL, so I would check the SQL log for our version.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How do @BatchSize, hibernate.default_batch_fetch_size and FetchMode.SUBSELECT differ in...": https://scrivio-interview-library.netlify.app/databases/jpa-performance/df74962043/