Senior
Are Hibernate relationships (OneToOne, OneToMany, ManyToOne, ManyToMany) eager or lazy by default?
sobes.tech AI
Answer from AI
In Hibernate, the default fetch behavior for associations is as follows:
- @OneToOne — by default EAGER (eager loading)
- @ManyToOne — by default EAGER
- @OneToMany — by default LAZY (lazy loading)
- @ManyToMany — by default LAZY
This means that when an entity is loaded, Hibernate immediately fetches related objects for OneToOne and ManyToOne, while for collections (OneToMany, ManyToMany), it loads them only when accessed.
If you want to change this behavior, you can explicitly specify fetch = FetchType.LAZY or FetchType.EAGER in the annotation.
Example:
@OneToMany(fetch = FetchType.EAGER)
private Set<Order> orders;