Hibernate Tutorial
Core Hibernate
Hibernate Mapping
Hibernate Annotations
Hibernate with Spring Framework
Hibernate with Database
Hibernate Log4j
Inheritance Mapping
Eager and lazy loading are important concepts in Hibernate and other ORM (Object-Relational Mapping) frameworks. They pertain to how related entities are loaded from the database when you fetch a particular entity. Let's delve deeper:
When an entity is fetched, all its related entities are loaded at the same time. This is done using JOIN queries.
Using JPA annotations in Hibernate:
@Entity public class User { @OneToMany(fetch = FetchType.EAGER) private List<Order> orders; // other fields, getters, setters... }
In this example, when you fetch a User
, all related Order
entities are loaded at the same time.
Related entities are not loaded when the main entity is fetched. Instead, they are loaded on-demand when you access the related entity for the first time.
LazyInitializationException
if the related entity is accessed outside of the Hibernate session context.Using JPA annotations in Hibernate:
@Entity public class User { @OneToMany(fetch = FetchType.LAZY) private List<Order> orders; // other fields, getters, setters... }
In this example, when you fetch a User
, the related Order
entities are not loaded. They will be loaded when you try to access them, like user.getOrders()
.
Default Settings: In Hibernate, the default fetch type for @OneToMany
and @ManyToMany
associations is LAZY
. For @OneToOne
and @ManyToOne
, it is EAGER
.
N+1 Problem: This problem can be mitigated using techniques like JOIN FETCH or using the Hibernate-specific BatchSize
annotation.
Understanding Use Cases: Always understand the use-case for data fetching. If you always need the related entities whenever you fetch the primary entity, EAGER
loading might make sense. If not, LAZY
loading is generally a better default, but always be aware of the potential pitfalls.
Session Context: Always ensure that lazy initialization happens within an active Hibernate session to avoid LazyInitializationException
. If you need to access the data outside of the session, you'll have to initialize the proxy or collection before ending the session, or consider other fetching strategies.
In conclusion, understanding eager and lazy loading is crucial for achieving optimal performance in Hibernate applications. Adjust your fetching strategy based on the specific needs of your application, and always test the performance implications of your choices.
Configuring lazy loading in Hibernate:
fetch
attribute in the association annotations (@OneToOne
, @OneToMany
, @ManyToOne
, @ManyToMany
). Setting fetch = FetchType.LAZY
enables lazy loading.@Entity public class ParentEntity { @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY) private List<ChildEntity> children = new ArrayList<>(); }
Avoiding N+1 Select Issue in Hibernate lazy loading:
@BatchSize
) and using JOIN FETCH
in HQL queries.@BatchSize
:@Entity public class ParentEntity { @OneToMany(mappedBy = "parent") @BatchSize(size = 10) private List<ChildEntity> children = new ArrayList<>(); }