Sobes.tech
Junior — Middle

Can you explain what FetchType.LAZY means in the context of data loading in ORM?

sobes.tech AI

Answer from AI

FetchType.LAZY is a strategy for loading related data in ORM (for example, in JPA/Hibernate), where related entities are not loaded immediately with the main entity, but only upon the first access to them. This helps optimize performance by reducing the amount of data loaded and the number of database queries.

For example, if you have an entity User with a collection orders, with LAZY loading, the list of orders will not be loaded when retrieving the user, but only when you call user.getOrders().

@Entity
public class User {
    @OneToMany(fetch = FetchType.LAZY)
    private List<Order> orders;
}

This helps avoid unnecessary resource consumption, especially if related data is not always needed.

Can you explain what FetchType.LAZY means in the… - sobes.tech