Complete the code to declare a many-to-many relationship in a Spring Boot entity.
import jakarta.persistence.*; import java.util.*; @Entity public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @[1] private Set<Course> courses = new HashSet<>(); // getters and setters }
The @ManyToMany annotation defines a many-to-many relationship between entities.
Complete the code to specify the join table for the many-to-many relationship.
import jakarta.persistence.*; import java.util.*; @Entity public class Student { @ManyToMany @[1]( name = "student_course", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "course_id") ) private Set<Course> courses = new HashSet<>(); // getters and setters }
The @JoinTable annotation defines the join table and join columns for a many-to-many relationship.
Fix the error in the code to correctly map the many-to-many relationship inverse side.
import jakarta.persistence.*; import java.util.*; @Entity public class Course { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; @ManyToMany(mappedBy = "[1]") private Set<Student> students = new HashSet<>(); // getters and setters }
The mappedBy attribute should match the name of the collection in the owning entity, which is courses in Student.
Fill both blanks to complete the code for the join table with cascade and fetch type.
import jakarta.persistence.*; import java.util.*; @Entity public class Student { @ManyToMany(cascade = CascadeType.[1], fetch = FetchType.[2]) @JoinTable( name = "student_course", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "course_id") ) private Set<Course> courses = new HashSet<>(); // getters and setters }
CascadeType.ALL applies all cascade operations. FetchType.LAZY delays loading related entities until needed.
Fill all three blanks to complete the bidirectional many-to-many relationship with proper annotations and collection types.
import jakarta.persistence.*; import java.util.*; @Entity public class Course { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; @ManyToMany(mappedBy = "[1]", cascade = CascadeType.[2], fetch = FetchType.[3]) private Set<Student> students = new HashSet<>(); // getters and setters }
The mappedBy should be 'courses' to match the owning side. Cascade type ALL applies all cascade operations. Fetch type LAZY delays loading until needed.