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.


How to Read @ManyToOne Correctly?

👉 "Many Orders belong to One Customer"


Breaking It Down in Simple Terms

  1. A single Customer can place multiple Orders → (@OneToMany in Customer).
  2. Each Order is linked to only one Customer → (@ManyToOne in Order).