FetchType is a Jakarta Persistence (JPA) enumeration that tells the persistence provider when to load an association or basic attribute: EAGER loads it immediately together with the owning entity, and LAZY loads it only when your code first accesses it. You set it with the fetch attribute of mappings such as @ManyToOne, @OneToMany and @Basic.
Choosing the right value is one of the biggest levers for JPA performance, because it decides how many SQL statements run and how much data moves between the database and your application.
The enum has exactly two constants:
| Constant | Meaning | Typical result |
|---|---|---|
FetchType.EAGER | The provider must load the data when the owner is loaded | More data per query, fewer surprises later |
FetchType.LAZY | The provider may delay loading until first access | Smaller initial query, extra queries on demand |
EAGER is a requirement on the provider. LAZY is only a hint: a provider may load the data eagerly anyway, and for basic attributes and to-one associations Hibernate needs bytecode enhancement to make them truly lazy.
If you do not set fetch, the JPA specification supplies a default that depends on the annotation.
| Mapping | Default |
|---|---|
@Basic | EAGER |
@ManyToOne | EAGER |
@OneToOne | EAGER |
@OneToMany | LAZY |
@ManyToMany | LAZY |
@ElementCollection | LAZY |
The eager defaults on @ManyToOne and @OneToOne are a frequent cause of slow applications. A query for 100 orders can trigger 100 extra selects for customers. Many teams set fetch = FetchType.LAZY explicitly on every to-one association.
For a lazy association Hibernate places a proxy (to-one) or a special collection implementation (to-many) in the entity. The first call that touches real data runs a select, and it needs an open persistence context.
LazyInitializationException means you accessed a lazy association after the session or entity manager was closed. Fixes, in order of preference:
join fetch, an entity graph, or a DTO projection.@Transactional service method.EAGER just to silence the exception.Spring Boot enables spring.jpa.open-in-view by default and logs a warning at startup. It keeps the persistence context open during web request rendering, so lazy loads in controllers or serializers work, but it hides N+1 queries and holds a database connection longer. Setting spring.jpa.open-in-view=false is the common production choice.
The mapping gives you the default. A query can override it for one use case, which is why lazy mappings are the recommended baseline.
With Spring Data JPA you can use an entity graph instead of changing the mapping:
This loads orders and segment in the same query for that method only, while the mapping stays lazy.
N+1 appears when one query returns N parents and each parent triggers one more query to load its association. It happens with eager to-one mappings in a query, and with lazy collections that you iterate over.
Ways to remove it:
join fetch or @EntityGraph for the associations the use case needs.@BatchSize or hibernate.default_batch_fetch_size so lazy loads are grouped into in (...) queries.When you join fetch a collection with pagination, Hibernate cannot apply the limit in SQL and paginates in memory with a warning, so page the parent ids first and fetch collections in a second query.
Use LAZY as the default for every association and fetch deliberately per use case. Consider EAGER only for a small, always-needed to-one association where the extra join is cheap, and be aware that you cannot make it lazy in a query later. Never put EAGER on a collection: it forces a load on every access of the parent, including find and every query result.
It depends on the mapping. @ManyToOne and @OneToOne default to EAGER, while @OneToMany, @ManyToMany and @ElementCollection default to LAZY.
No. The specification treats it as a hint. Hibernate honours it for collections and for to-one associations through proxies, but a to-one @OneToOne on the non-owning side may still load eagerly without bytecode enhancement.
You read a lazy association after the persistence context closed. Load the data inside the transaction using join fetch, an entity graph or a DTO.
You can make a lazy association load eagerly with join fetch or an entity graph. The reverse is not possible for an EAGER mapping that is loaded by a query.
FetchType decides when JPA loads data: EAGER right away, LAZY on first access. Remember the defaults (to-one eager, to-many lazy), set LAZY explicitly, and shape each query with join fetch, entity graphs, batch fetching or DTOs. That combination avoids both LazyInitializationException and N+1 selects.