Complete the code to declare a many-to-one relationship in a Spring Boot entity.
import jakarta.persistence.*; @Entity public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @[1] private Customer customer; // getters and setters }
The @ManyToOne annotation defines a many-to-one relationship from Order to Customer.
Complete the code to specify the foreign key column name for the many-to-one relationship.
import jakarta.persistence.*; @Entity public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne @JoinColumn(name = "[1]") private Customer customer; // getters and setters }
The @JoinColumn annotation specifies the foreign key column name in the Order table that references the Customer entity.
Fix the error in the code to correctly map the many-to-one relationship with lazy loading.
import jakarta.persistence.*; @Entity public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne(fetch = FetchType.[1]) @JoinColumn(name = "customer_id") private Customer customer; // getters and setters }
Using FetchType.LAZY delays loading the related Customer until it is accessed, improving performance.
Fill both blanks to complete the code for a bidirectional many-to-one relationship.
import jakarta.persistence.*; import java.util.List; @Entity public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @[1](mappedBy = "customer") private List<Order> [2]; // getters and setters }
The @OneToMany annotation on Customer defines the list of orders. The field name orders holds these related Order objects.
Fill all three blanks to complete the code for an Order entity with a many-to-one relationship and cascade persist.
import jakarta.persistence.*; @Entity public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne(cascade = CascadeType.[1], fetch = FetchType.[2]) @JoinColumn(name = "[3]") private Customer customer; // getters and setters }
The cascade type PERSIST ensures saving the Customer when saving the Order. The fetch type LAZY delays loading the customer until needed. The join column customer_id is the foreign key.