The @Fetch annotation (org.hibernate.annotations.Fetch) is a Hibernate-specific annotation that controls how Hibernate loads an association from the database: with a SQL join, with a separate select, or with a subselect. It complements the standard JPA FetchType, which only decides when data is loaded (eagerly or lazily), not how the SQL is built.
Because @Fetch is part of Hibernate and not of Jakarta Persistence, using it ties your mapping to Hibernate. The examples below target Hibernate 6.x with the jakarta.persistence namespace.
JPA gives you one switch per association: FetchType.EAGER or FetchType.LAZY. It says nothing about the SQL. Hibernate adds @Fetch with a FetchMode value so you can choose the loading strategy.
| Concern | Set by | Values |
|---|---|---|
| When the data is loaded | JPA fetch attribute | EAGER, LAZY |
| How the data is loaded | Hibernate @Fetch | JOIN, SELECT, SUBSELECT |
Hibernate loads the association in the same SQL statement as the owner, using an outer join. This is the shape of an eager load by identifier, such as entityManager.find(Author.class, 1L). It is one round trip, but for collections the result set repeats the parent columns for every child row.
Hibernate loads the association with an additional select when it is needed (lazy) or right after the owner is loaded (eager). This is the default for collections and is the source of the N+1 problem when you iterate over many parents.
Hibernate loads the collections of all parents returned by the original query with a single second query that reuses the original query as a subselect. It only applies to collections. Loading 50 authors and then touching books on the first one loads the books of all 50 in one statement.
The most common surprise is that @Fetch(FetchMode.JOIN) does not rewrite JPQL, HQL or Criteria queries. Those queries run exactly as written, and any eager association not joined in the query is loaded with follow-up selects. The join strategy takes effect when you load an entity by its identifier (find, getReference followed by initialization, or Session.get).
When you want a join in a query, say so explicitly with join fetch.
In Hibernate 6 the distinct keyword is no longer needed to remove duplicate parents, because Hibernate deduplicates entities in the result list. It is still harmless.
N+1 happens when one query loads N parents and then Hibernate issues one extra query per parent to load a lazy association. You can see it by enabling SQL logging with spring.jpa.show-sql=true or by counting statements in a test.
| Situation | Good choice |
|---|---|
| One entity loaded by id, small to-one association | FetchMode.JOIN |
| List of parents, you always touch the collection | FetchMode.SUBSELECT or join fetch in the query |
| List of parents, you sometimes touch the collection | Lazy plus @BatchSize |
| Different screens need different shapes | Lazy mapping plus entity graphs or per-query join fetch |
@BatchSize loads lazy associations for several parents at once using an in (...) list, with no join and no subselect. You can set it globally with the property hibernate.default_batch_fetch_size, for example to 16 or 32.
Note that @BatchSize and @Fetch(FetchMode.SUBSELECT) are alternative strategies for the same association, so pick one.
The two settings interact, and some combinations make little sense:
FetchType.LAZY with FetchMode.JOIN: the association stays a lazy proxy or lazy collection for queries, and the join is only used for eager loading paths. In practice the lazy setting wins when the association is accessed from a loaded entity.FetchType.EAGER with FetchMode.SELECT: the association is loaded right after the owner with a second statement.FetchType.EAGER with FetchMode.JOIN on several collections: a join across multiple bags can produce huge cartesian products and a MultipleBagFetchException. Use Set or @OrderColumn for collections, or fetch them in separate queries.Prefer LAZY everywhere and decide per use case how to load data. Static eager mappings are hard to undo later.
No. @Fetch belongs to org.hibernate.annotations. Standard JPA only offers FetchType, plus entity graphs for per-query fetch plans.
Not by itself. Queries ignore the mapped fetch mode for joining, so add join fetch, an entity graph (@EntityGraph in Spring Data JPA), or use @BatchSize.
For collections, Hibernate uses SELECT. For to-one associations loaded by id, an eager association is loaded with a join.
Yes, JOIN and SELECT are valid for to-one associations. SUBSELECT applies to collections only.
@Fetch selects the SQL strategy Hibernate uses for an association: JOIN, SELECT or SUBSELECT. It is independent of FetchType, it is ignored for joining in HQL, JPQL and Criteria queries, and it is Hibernate-specific. Keep associations lazy, measure the generated SQL, and use join fetch, entity graphs, @BatchSize or SUBSELECT to remove N+1 where you actually need the data.