Sobes.tech
Senior

What are the default fetch types for entity relationships in Hibernate (EAGER or LAZY)?

sobes.tech AI

Answer from AI

In Hibernate, the default fetch types for entity relationships are:

  • @ManyToOne and @OneToOne — by default EAGER (eager loading). This means related entities are loaded immediately along with the main entity.
  • @OneToMany and @ManyToMany — by default LAZY (lazy loading). Related collections are loaded only when accessed.

This behavior can be overridden using the @FetchType.LAZY or @FetchType.EAGER annotations.

Example:

@Entity
class Order {
    @ManyToOne(fetch = FetchType.EAGER) // by default
    private Customer customer;

    @OneToMany(fetch = FetchType.LAZY) // by default
    private Set<OrderItem> items;
}