What is the best way to link products to an order in a Spring application?

Viewed 3193

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.

1 Answers

How orders are generally implemented are with line_items. Orders have multiple line_items. Each line_item should have the product_id from the Product table, the quantity, and the order_id from the Order table.

  +------------+       +-----------------+        +---------------+
  |   Order    |       |    Line_Item    |        |    Product    |
  +------------+       +-----------------+        +---------------+
  | id         |       | id              |        | id            |
  | date       |       | quantity        |        | name          |
  | paid       |       | price           |        | description   |
  +------------+       | product_id      |        +---------------+
                       | order_id        |
                       +-----------------+

So the Line_item table has a many-to-one relationship with the Product and a many-to-one with the Order.

When a user adds an item to the cart, a new line_item is created. With this set up, products can easily be navigated to from an Order. All line_items are tightly coupled with an Order, and the should be cascade deleted on an Order being deleted. An order can easily be cleared just by removing all line_items associated with that Order.

In Code, it would look like

class Order {
    @Id
    long id;
    Date date;
    boolean paid;

    @OneToMany
    List<LineItem> lineItems;
}

class LineItem {
    @Id
    long id;
    int quantity;

    @ManyToOne
    Order order;

    @ManyToOne
    Product product;
}

class Product {
    @Id
    long id;
    String name;
    String description;
    BigDecimal price;

    // Product does not need
    // to know about line_items
}

Here's how I implemented a shopping application before

There is a concept of Shopping Cart and Order. These two are different, but contain pretty much the same information. It may seem redundant, but I felt this is the cleanest way to represent reality. A user should only ever have one shopping cart, but can have multiple orders. When a user checks out, all the information from the shopping cart is transferred into a new Order and the shopping cart is cleared.

Entities

Order       Order_Item      Product      Shopping_Cart     Cart_Item
-----       ----------      -------      -------------     ---------
id          id              id           id                id
date        quantity        name         user_id           quantity
user_id     product_id      description                    product_id
            order_id        price                          cart_id

You can see that Order and Order_Item are similar to Shopping_Cart and Cart_Item. When a user begins shopping, a shopping cart is initialized for them. When they add a product to the cart, Cart_Items are created, which are associated with the Shopping_Cart. When the user checks out, an Order is created, and the Shopping_Cart data is transferred to the Order, and all Cart_Items are transfers to Order_Items associated with the Order. The Shopping_Cart is then cleared by simply deleting all Cart_Items.

Related