I am developing a simple Spring Boot application offering a REST service, to allow clients to login and order some products.
In my MySQL database I have the following tables :
Client Seller Shop Product Order
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.
I will include only the relationship's part to give you a clearer idea about my code :
For Product model class:
@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;
For Order model class :
@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;
For Shop model class :
@JoinColumn(name = "user_id", nullable = false)
@ManyToOne(fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private User user;
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 :
First solution:
- I create a new table named "ordred_products"
- 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.
Second solution:
- I keep just the orders table and add a Set of ordered products to it.
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.
Thank you so much.