title: What is the best way to link products to an order in a Spring application?
body: <p>I am developing a simple Spring Boot application offering a REST service, to allow clients to login and order some products.</p>
  
  <p>In my MySQL database I have the following tables :</p>
  
  <p>Client
  Seller
  Shop
  Product
  Order</p>
  
  <p>The user can select multiple products, add them to the cart and then clicks on " Proceed to checkout".
  Before this part, everything was perfect but now I am not sure how to proceed.</p>
  
  <p>I will include only the relationship's part to give you a clearer idea about my code :</p>
  
  <p><strong>For Product model class:</strong></p>
  
  <pre><code>@JoinColumn(name = "shop_id", nullable = false)
  @ManyToOne(fetch = FetchType.LAZY)
  @OnDelete(action = OnDeleteAction.CASCADE)
  @JsonIgnore
  private Shop shop;
  
  @JoinColumn(name = "order_id", nullable = false)
  @ManyToOne(fetch = FetchType.LAZY)
  @OnDelete(action = OnDeleteAction.CASCADE)
  @JsonIgnore
  private Order order;
  </code></pre>
  
  <p>For Order model class :</p>
  
  <pre><code>@JoinColumn(name = "user_id", nullable = false)
  @ManyToOne(fetch = FetchType.LAZY)
  @OnDelete(action = OnDeleteAction.CASCADE)
  @JsonIgnore
  private User user;
  
  @JoinColumn(name = "shop_id", nullable = false)
  @ManyToOne(fetch = FetchType.LAZY)
  @OnDelete(action = OnDeleteAction.CASCADE)
  @JsonIgnore
  private Shop shop;
  </code></pre>
  
  <p>For Shop model class :</p>
  
  <pre><code>@JoinColumn(name = "user_id", nullable = false)
  @ManyToOne(fetch = FetchType.LAZY)
  @OnDelete(action = OnDeleteAction.CASCADE)
  @JsonIgnore
  private User user;
  </code></pre>
  
  <p>Now I need to make a REST POST call, to create an order, these are the solutions are could think about but I am not sure if either of them is correct :</p>
  
  <p><strong>First solution:</strong></p>
  
  <ul>
  <li>I create a new table named "ordred_products"</li>
  <li>When a user makes an order I send a POST request to create the order, and then I make a POST request to create an "ordered_products" element, which will have the ordered products as a Set, but I don't know how to map them.</li>
  </ul>
  
  <p><strong>Second solution:</strong></p>
  
  <ul>
  <li>I keep just the orders table and add a Set of ordered products to it.</li>
  </ul>
  
  <p>I would be thankful is someone can explain to me the right way to do this as I am just learning REST and relationships between entities.</p>
  
  <p>Thank you so much.</p>