Spring Data JPA Test 1

    Question 1DATA JPA - Hibernate ORM Purpose

    What is the main purpose of Hibernate ORM in a Spring Data JPA context?

    Question 2DATA JPA - @OneToOne Mapping

    Which of the following best describes a @OneToOne relationship in JPA?

    Question 3DATA JPA - CrudRepository vs JpaRepository

    Which statement correctly describes the relationship between CrudRepository and JpaRepository?

    Question 4DATA JPA - Default Fetch Types

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

    Question 5DATA JPA - Basics of @Transactional

    What is the primary purpose of the @Transactional annotation in Spring Data JPA?

    Question 6DATA JPA - JPA Entity Annotation

    Which annotation is used to mark a class as a JPA entity in Hibernate ORM?

    Question 7DATA JPA - Code Snippet (@OneToMany)

    What does the following mapping represent?

    @Entity public class Department { @Id private Long id; @OneToMany(mappedBy = "department") private List<Employee> employees; }

    Question 8DATA JPA - Code Snippet (CrudRepository)

    What will the following repository provide?

    public interface StudentRepository extends CrudRepository<Student, Long> { }

    Question 9DATA JPA - Default Collection Fetching

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

    Question 10DATA JPA - Default Rollback Behavior

    By default, @Transactional in Spring rolls back on which type of exceptions?

    Question 11DATA JPA - Code Snippet (Entity Mapping)

    What will the following entity map to in the database?

    @Entity @Table(name = "students") public class Student { @Id private Long id; private String name; }

    Question 12DATA JPA - @ManyToOne Mapping

    In JPA, what is the typical use of @ManyToOne annotation?

    Question 13DATA JPA - Paging and Sorting

    Which repository interface provides built-in methods for pagination and sorting?

    Question 14DATA 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 15DATA JPA - Code Snippet (Rollback Example)

    What will happen to the database changes in the following method?

    @Transactional public void saveData() { userRepo.save(new User("John")); if (true) { throw new RuntimeException("Failure!"); } }

    Question 16DATA JPA - Entity Life Cycle States

    Which of the following is not a valid JPA entity lifecycle state?

    Question 17DATA JPA - Code Snippet (@ManyToMany)

    What is the effect of the following mapping?

    @Entity public class Student { @ManyToMany @JoinTable( name = "student_course", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "course_id") ) private List<Course> courses; }

    Question 18DATA JPA - Code Snippet (@Query Custom JPQL)

    What does the following method do?

    public interface EmployeeRepository extends JpaRepository<Employee, Long> { @Query("SELECT e FROM Employee e WHERE e.department = ?1") List<Employee> findByDepartment(String department); }

    Question 19DATA JPA - LazyInitializationException

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

    Question 20DATA JPA - Checked Exception Handling

    What will happen in this case?

    @Transactional public void saveData() throws IOException { userRepo.save(new User("Alice")); throw new IOException("Checked Exception!"); }