Sobes.tech
Junior — Middle

What types of data loading methods exist for related entities in a system?

sobes.tech AI

Answer from AI

To load data into a system when dealing with related entities (for example, in relational databases or ORM), the main methods are:

  1. Eager loading — all related entities are loaded immediately along with the main entity. This is convenient when all data is needed at once but can lead to overloading.

  2. Lazy loading — related entities are loaded only when accessed for the first time. This saves resources if related data is not always needed.

  3. Explicit loading — loading related entities explicitly through separate queries when necessary.

  4. Batch loading — loading related entities in groups to reduce the number of database queries.

Example in JPA (Java Persistence API):

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

Here, items will be loaded lazily, i.e., when order.getItems() is first accessed.

The choice of method depends on performance requirements and data volume.