SteelReed

Search

Search posts, questions…

JPA & HibernateQuestion 17 / 24

Middle

How do FetchType LAZY and EAGER differ, and what should the default be?

Short answer

LAZY loads a relation only when code touches it; EAGER loads it immediately with the parent. By default @ManyToOne/@OneToOne are EAGER and collections are LAZY. In practice: make every relation LAZY and load what each use case needs.

yesLAZYquery N orderscustomer EAGER?N extra SELECTsN+1load only when touchedJOIN FETCH when needed

3 points to say:

  1. To-one relations are EAGER by default.
  2. EAGER with JPQL usually becomes N+1.
  3. Touching LAZY data after the session closes fails.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.