🚫 1. Detached entities or closed session
- Batch fetching works only if multiple lazy proxies are accessed within the same persistence context (i.e., the same open Hibernate
Session
or JPAEntityManager
). - If you load entities in one transaction but access their lazy associations later, outside of that session, you’ll get
LazyInitializationException
instead of batch fetching.
🚫 2. Single proxy accessed
- Batch fetching triggers only if Hibernate sees multiple proxies of the same type waiting for initialization.
- If you access just one lazy association, batching has nothing to batch → it’s still a single query.
🚫 3. Associations already eagerly loaded
- If you fetch an association with
JOIN FETCH
or it’s configured withFetchType.EAGER
, there’s no lazy loading left → batch fetching has no effect.
🚫 4. Mixed session contexts
- If you scatter your entity access across multiple transactions or
EntityManager
s, batch fetching can’t group proxies because it only considers proxies in the same session.
🚫 5. Proxies of different types
- Batch fetching only batches same-type lazy proxies.
- E.g., if you have 20
Author
proxies and 20Publisher
proxies, they’re batched separately — not together. - So if you only have a few of each type, batching may have limited impact.
- E.g., if you have 20
🚫 6. Direct queries ignoring proxies
- If you manually run queries on child entities (e.g.,
SELECT b FROM Book b WHERE ...
) instead of accessing them through lazy proxies → batch fetching never triggers.
🚫 7. Using DTO projections
- If you bypass entity mappings entirely (e.g., mapping query results to DTOs), Hibernate doesn’t manage proxies → batch fetching doesn’t apply.
⚠️ Summary:
Batch fetching works only on lazy proxies accessed in the same session, and only if there’s more than one proxy of the same type to batch together.