Junior — Middle
How does the FetchType EAGER data loading strategy work in JPA?
sobes.tech AI
Answer from AI
The FetchType.EAGER loading strategy in JPA means that related entities are loaded from the database immediately along with the main entity. For example, if you have an entity Order with a @OneToMany relationship to OrderItem and specify fetch = FetchType.EAGER, then when loading an order from the database, all related order items will also be loaded.
This is convenient when related data is always needed, but can lead to excessive loading and reduced performance if there are many related data and they are not always used.
Example:
@Entity
public class Order {
@OneToMany(fetch = FetchType.EAGER)
private Set<OrderItem> items;
}
In this case, when loading Order, JPA will perform an additional query or join to immediately retrieve all OrderItem.