orders; // A customer has many orders } @Entity public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne @JoinColumn(name = "customer_id") private Customer customer; // An order belongs to one customer }"> orders; // A customer has many orders } @Entity public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne @JoinColumn(name = "customer_id") private Customer customer; // An order belongs to one customer }"> orders; // A customer has many orders } @Entity public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne @JoinColumn(name = "customer_id") private Customer customer; // An order belongs to one customer }">
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
private List<Order> orders; // A customer has many orders
}
@Entity
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "customer_id")
private Customer customer; // An order belongs to one customer
}
At first glance, reading @ManyToOne in the Order entity might make it seem like one order can have multiple customers, but that's not what it actually means.
@ManyToOne Correctly?@ManyToOne in Order means:
Order instances (many orders)Customer (each order has only one customer).Order has many customers."Customer can place multiple Orders → (@OneToMany in Customer).Order is linked to only one Customer → (@ManyToOne in Order).