Lazy vs Eager Loading & N+1 Problem

    Question 1DATA JPA - Default Fetch Types

    What is the default fetch type for `@ManyToOne` and `@OneToOne` relationships in JPA?

    Question 2DATA JPA - Default Collection Fetching

    What is the default fetch type for `@OneToMany` and `@ManyToMany` associations?

    Question 3DATA JPA - Code Snippet (Lazy Loading)

    What happens when accessing `department.getEmployees()` if `employees` is mapped as `LAZY`?

    @Entity public class Department { @OneToMany(mappedBy = "department", fetch = FetchType.LAZY) private List<Employee> employees; }

    Question 4DATA JPA - LazyInitializationException

    When does a `LazyInitializationException` occur in JPA/Hibernate?

    Question 5DATA JPA - Code Snippet (EAGER Loading)

    What will happen in the following mapping?

    @Entity public class Employee { @ManyToOne(fetch = FetchType.EAGER) private Department department; }

    Question 6DATA JPA - N+1 Query Problem Definition

    What is the N+1 problem in JPA/Hibernate?

    Question 7DATA JPA - Code Snippet (N+1 Example)

    What performance issue will occur here if `employees` is `LAZY`?

    List<Department> departments = departmentRepo.findAll(); for (Department dept : departments) { System.out.println(dept.getEmployees().size()); }

    Question 8DATA JPA - Solving N+1 with Fetch Join

    Which query strategy can help avoid the N+1 problem?

    Question 9DATA JPA - Code Snippet (Fetch Join Solution)

    What does the following repository method achieve?

    @Query("SELECT d FROM Department d JOIN FETCH d.employees") List<Department> findAllWithEmployees();

    Question 10DATA JPA - Best Practices for Fetch Strategies

    Which of the following is considered the best practice in designing fetch strategies?