Complete the code to declare a one-to-many relationship in a Spring Boot entity.
import jakarta.persistence.*; @Entity public class Department { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @[1] private List<Employee> employees; // getters and setters }
The @OneToMany annotation is used to define a one-to-many relationship from the Department entity to the Employee entity.
Complete the code to specify the mappedBy attribute in the @OneToMany annotation.
@OneToMany(mappedBy = "[1]") private List<Order> orders;
The mappedBy attribute refers to the field name in the child entity that owns the relationship. Here, it should be the field in Order that points back to Customer.
Fix the error in the @OneToMany annotation to enable cascade operations.
@OneToMany(cascade = CascadeType.[1])
private Set<Item> items;Using CascadeType.ALL ensures that all cascade operations (persist, merge, remove, refresh, detach) are applied to the related entities.
Fill both blanks to complete the bidirectional @OneToMany and @ManyToOne relationship.
public class Parent { @OneToMany(mappedBy = "[1]", cascade = CascadeType.ALL) private List<Child> children; } public class Child { @[2] private Parent parent; }
The mappedBy in Parent points to the parent field in Child. The Child side uses @ManyToOne to link back to Parent.
Fill all three blanks to complete the @OneToMany relationship with fetch type and orphan removal.
@OneToMany(mappedBy = "[1]", fetch = FetchType.[2], orphanRemoval = [3]) private List<Comment> comments;
The mappedBy is the field in Comment that points to Post. FetchType.LAZY delays loading until needed. Orphan removal true deletes comments removed from the list.