Sobes.tech
Junior — Middle

Can you list the types of data loading in JPA and explain their differences?

sobes.tech AI

Answer from AI

In JPA, there are two main types of data loading:

  • Eager loading — related entities are loaded immediately along with the main entity. This is convenient when related data is definitely needed, but can lead to over-fetching and reduced performance.

  • Lazy loading — related entities are loaded only upon first access. This saves resources if related data is not always needed, but requires the EntityManager session to be open at the time of access.

Example of an annotation in JPA:

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

Here, items will be loaded only when accessed, not immediately when the order is loaded.

Can you list the types of data loading in JPA and… - sobes.tech