Hibernate - Eager/Lazy Loading

Last Updated : 28 Mar, 2026

Eager and Lazy Loading in Hibernate are strategies that define how related data is fetched from the database. They control whether associated entities are loaded immediately with the parent object or only when needed, helping balance performance and resource usage.

  • Eager Loading fetches related data instantly along with the main entity
  • Lazy Loading delays fetching until the data is actually accessed
  • Choosing the right strategy improves performance, memory usage, and query efficiency
fetch_types

Note: You can specify the fetch type of an association by using the fetch attribute of the @OneToMany, @ManyToOne, @OneToOne, or @ManyToMany annotations.

LAZY Loading

FetchType.LAZY is used to delay the loading of associated entities until they are actually accessed. This helps optimize performance by avoiding unnecessary data retrieval at the time of the main entity loading.

  • Loads associated data only on first access, not during initial fetch.
  • Default for collection associations like @OneToMany and @ManyToMany.
  • Can cause extra queries (N+1 problem) if not handled properly.
Java
@Entity
public class Employee {
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "address_id")
    private Address address;

    // other fields and methods
}

Note: In this example, the Address entity associated with an Employee will be fetched lazily when it is accessed for the first time.

EAGER Loading

FetchType.EAGER means the associated entity is loaded immediately along with the main entity. It ensures that related data is always available at the time of fetching the parent entity.

  • Fetches associated data instantly with the parent entity.
  • Useful when related data is always required.
  • Can impact performance if associated data is large or not always needed.
Java
@Entity
public class Employee {
    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "address_id")
    private Address address;

    // other fields and methods
}

Note: In this example, the Address entity associated with an Employee will be fetched eagerly when the Employee is loaded from the database.

Eager Loading Vs Lazy Loading

FeatureEager LoadingLazy Loading
DefinitionLoads related data immediately with the parent entityLoads related data only when it is accessed
Fetching TimeAt the time of initial queryOn-demand (when getter is called)
PerformanceCan be slower if unnecessary data is fetchedImproves performance by loading only required data
Memory UsageHigher (loads all related data)Lower (loads data only when needed)
Use CaseWhen related data is always requiredWhen related data is not always needed
AnnotationFetchType.EAGERFetchType.LAZY
Database QueriesFewer queries but heavier dataMore queries but optimized data loading
RiskOver-fetching dataMay cause LazyInitializationException
Comment