@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "author", fetch = FetchType.EAGER)
private List<Book> books = new ArrayList<>();
}Author author = entityManager.find(Author.class, 1L);
// 이 시점에서 Author와 연관된 모든 Book 엔티티들이 데이터베이스에서 불러와짐@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "author", fetch = FetchType.LAZY)
private List<Book> books = new ArrayList<>();
}Author author = entityManager.find(Author.class, 1L);
// 이 시점에서는 Author만 불러와짐
List<Book> books = author.getBooks();
// 이 시점에서야 관련된 Book 엔티티들이 데이터베이스에서 불러와짐@OneToMany : 기본적으로 지연 로딩이 적용@OneToMany(fetch = FetchType.LAZY)fetch = FetchType.LAZY가 지연 로딩, fetch = FetchType.EAGER가 즉시 로딩@ManyToOne : 기본적으로 즉시 로딩이 적용되지만, 지연 로딩으로 변경할 수 있음@ManyToOne(fetch = FetchType.LAZY)@OneToOne 및 @ManyToMany : 기본적으로 지연 로딩이 적용