Video in progress
What is the default fetch type for each kind of association?
@ManyToOne and @OneToOne default to EAGER. @OneToMany and @ManyToMany default to LAZY. In other words, associations that point to a single object are eager and collections are lazy.
📖 Answer
This is a rule from the JPA specification, and Hibernate follows it. Collections can be large, so they were made lazy; a single object was considered "cheap".
The trouble is that this assumption often turns out wrong:
@ManyToOne // eager: every OrderItem loads its Product too
private Product product;
If you read 200 items with select i from OrderItem i, Hibernate may send an extra SELECT for each product, because the query has no join.
That's why many teams have a rule: always write an explicit fetch = FetchType.LAZY on @ManyToOne and @OneToOne.
There's a catch with the inverse side of @OneToOne (the side with mappedBy): Hibernate can't always honor LAZY there, because it still has to look in the database to know whether the row exists. This comes up a lot in Middle-level questions.
Sign up to mark studied questions and save your progress.