Spring Data JPA Test 2

    Question 1DATA JPA - Code Snippet (Entity Manager Persist)

    What happens when the following code executes?

    Student student = new Student(); student.setId(1L); student.setName("Alex"); entityManager.persist(student);

    Question 2DATA JPA - Cascade Types

    Which cascade type ensures that when a parent entity is removed, its related child entities are also deleted?

    Question 3DATA JPA - Derived Query Methods

    Which of the following is a valid Spring Data JPA derived query method?

    Question 4DATA JPA - Code Snippet (EAGER Loading)

    What will happen in the following mapping?

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

    Question 5DATA JPA - Propagation Behavior

    Which propagation type ensures that if a transaction already exists, the method joins it; otherwise, a new one is created?

    Question 6DATA JPA - Entity Manager merge() Method

    What does entityManager.merge(entity) do in JPA?

    Question 7DATA JPA - Code Snippet (FetchType.LAZY vs EAGER)

    What is the default fetch type for a @OneToMany relationship in JPA?

    @OneToMany(mappedBy = "department") private List<Employee> employees;

    Question 8DATA JPA - Code Snippet (Pagination with Pageable)

    What is the purpose of Pageable in the following repository method?

    Page<Student> findByDepartment(String department, Pageable pageable);

    Question 9DATA JPA - N+1 Query Problem Definition

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

    Question 10DATA JPA - Code Snippet (REQUIRES_NEW)

    What happens in this case?

    @Transactional public void processOrder() { saveOrder(); // REQUIRED saveAudit(); // REQUIRES_NEW }

    Question 11DATA JPA - Code Snippet (Remove Entity)

    What will happen when this code executes?

    Student student = entityManager.find(Student.class, 1L); entityManager.remove(student);

    Question 12DATA JPA - Bidirectional Relationships

    In a bidirectional relationship, why is the mappedBy attribute important?

    Question 13DATA JPA - @Modifying with @Query

    Which of the following is required when executing update or delete queries with @Query?

    Question 14DATA 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 15DATA JPA - Isolation Levels

    Which isolation level prevents dirty reads but allows non-repeatable reads?

    Question 16DATA JPA - @Table Annotation

    What is the role of the @Table annotation in JPA?

    Question 17DATA JPA - Orphan Removal

    What happens when orphanRemoval = true is set on a relationship?

    Question 18DATA JPA - Code Snippet (@Modifying Example)

    What does the following method do?

    @Modifying @Transactional @Query("UPDATE Employee e SET e.salary = e.salary * 1.1 WHERE e.department = ?1") int increaseSalaryByDept(String department);

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

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

    Question 20DATA JPA - Code Snippet (Transactional ReadOnly)

    What effect does @Transactional(readOnly = true) have?

    @Transactional(readOnly = true) public List<User> getAllUsers() { return userRepo.findAll(); }