Complete the code to define a one-to-many relationship in JPA.
@Entity public class Author { @Id private Long id; @OneToMany(mappedBy = [1]) private List<Book> books; }
The mappedBy attribute refers to the field name in the related entity that owns the relationship. Here, it should be author as defined in the Book entity.
Complete the code to specify the owning side of a many-to-one relationship.
@Entity public class Book { @Id private Long id; @ManyToOne @JoinColumn(name = [1]) private Author author; }
The @JoinColumn specifies the foreign key column in the Book table that links to the Author. It is commonly named author_id.
Fix the error in the code to correctly define a bidirectional one-to-one relationship.
@Entity public class User { @Id private Long id; @OneToOne(mappedBy = [1]) private Profile profile; } @Entity public class Profile { @Id private Long id; @OneToOne @JoinColumn(name = "user_id") private User user; }
The mappedBy in User should refer to the field name in Profile that owns the relationship, which is user.
Fill both blanks to create a many-to-many relationship with a join table.
@Entity public class Student { @Id private Long id; @ManyToMany @JoinTable(name = [1], joinColumns = @JoinColumn(name = [2])) private Set<Course> courses; }
The join table is commonly named combining both entities, e.g., student_course. The joinColumns refers to the foreign key column for the owning entity, here student_id.
Fill all three blanks to define a unidirectional one-to-many relationship with cascade and fetch type.
@Entity public class Department { @Id private Long id; @OneToMany(cascade = [1], fetch = [2]) @JoinColumn(name = [3]) private List<Employee> employees; }
CascadeType.ALL applies all cascade operations. FetchType.LAZY delays loading employees until needed. The join column department_id links employees to the department.