Explain Codes LogoExplain Codes Logo

Default fetch type for one-to-one, many-to-one and one-to-many in Hibernate

java
lazy-loading
eager-loading
hibernate
Nikita BarsukovbyNikita Barsukov·Nov 9, 2024
TLDR
@OneToOne // Implicit: fetch = FetchType.EAGER // Like instant coffee; get it now! private RelatedEntity otoEntity; @ManyToOne // Implicit: fetch = FetchType.EAGER // Like calling an Uber; you need it right away! private RelatedEntity mtoEntity; @OneToMany // Implicit: fetch = FetchType.LAZY // Like making a drip coffee; take your time. private Set<RelatedEntity> otmEntities;

In Hibernate, associations @OneToOne and @ManyToOne come instantaneously (i.e., are eagerly fetched by default), while @OneToMany demand more patience (i.e., are lazily fetched by default, loading them when accessed, not immediately).

Decoding fetch types

Understand the consequences of EAGER and LAZY fetching. In a rush? EAGER loads related entities in tandem with the primary entity. Got some time? LAZY waits till the association is explicitly accessed, conserving initial load resources.

The blessing of an override

Afraid of the default? Hibernate allows you to override fetch strategies:

@OneToMany(fetch = FetchType.EAGER) private Set<RelatedEntity> otmEntities;

Working with proxies

Hibernate road signs: By default, expect a detour with LAZY loading via proxy object representation, focussing on the identifier, not the entity data. Useful for lonely One-To-One and Many-To-One relationships.

Buckle your fetch seatbelts

Evaluate fetch strategies to enhance application performance. Keep it LAZY for associations unless there's a compelling case for EAGER.

Fine-tuning fetch strategies

Fetch is more than fetch types. Unpack fetch=JOIN and fetch=SELECT, and how they affect the mapping override.

'join' vs 'select' - The fetch showdown

fetch='JOIN' is an EAGER beaver loading associated entities disregarding lazy='true'. On the other hand, fetch='SELECT' is more considerate, using an isolated SELECT to load related entities, making it a good fit for LAZY moments.

Dive into Hibernate's documentation and User Guide since its behavior may vary from expectations

Deciphering default fetch types

For collections, the default fetch strategy is select, while single-valued associations prefer join.

Developer's cheat sheet

EAGER vs LAZY - The ultimate decision

Choices make or mar an application. Opt for EAGER or LAZY loads based on usage patterns and performance impacts.

Beware of N+1 and LazyInitializationException potholes

EAGER pitfalls include the infamous N+1 select issue, while LAZY can trip up with unexpected LazyInitializationException if the Hibernate session is closed before the data is fetched.

Harness Hibernate's power

Unleash the beast; use Hibernate's batch fetching, sub-select fetching, and fetch profiles to hone your application's data-fetching strategies.

Keep your skillset updated

Hibernate evolves. Ensure your knowledge does too. Confirm behaviors with the specific Hibernate version you're using.