This controls when the related data (Role) is loaded from the database.
There are two options:
- Don’t load the
Roledata immediately when you load aUser. - Only load it when it is accessed (i.e., when you call
user.getRole()).
This is the default for
@OneToMany, but not for@ManyToOne.
- Load the
Roleimmediately along with theUser.
Because it:
- Avoids loading unnecessary data
- Saves memory and improves performance
- Makes queries faster when you don’t always need the related entity
Lazy loading can lead to the N+1 problem or LazyInitializationException if you're not careful.
User user = userRepo.findById(1L); // role is not fetched yet
entityManager.close(); // session is closed
user.getRole().getName(); // ❌ Throws LazyInitializationException!Because role is fetched lazily, and the session is already closed.
Use LAZY by default, but:
- Use
JOIN FETCHor@EntityGraphwhen you know you’ll need the related data - Be mindful of when the session is open
@Entity
public class User {
@ManyToOne(fetch = FetchType.LAZY)
private Role role;
}This means:
- A user has one role
- The role will only be fetched from DB when needed