title: Shopping Cart Application on Spring Boot with Hibernate/JPA
body: <p>I am creating shopping cart Spring Boot application with Hibernate and JPA. I have OneToMany mapping between CART and Product entity.
  My scenario is, User can add same product multiple times in the cart. If the same product is added more than once just need to increment the quantity by 1. I have created below entities.</p>
  <pre class="lang-java prettyprint-override"><code>public class Cart {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = &quot;cart_id&quot;)
      private int cartId;
  
      double price;
  
      int qty;
  
      private long user_id;
  
      @OneToMany(fetch = FetchType.EAGER)
      private List&lt;Product&gt; products;
  
      @OneToMany(mappedBy = &quot;cart&quot;, fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
      private List&lt;Quantity&gt; quantities;
  
      public Cart() {
  
      }
  }
  
  @Entity
  public class Quantity {
      
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private int id;
      
      private int productId;
       
      private int quantity;
  
      @ManyToOne(cascade=CascadeType.ALL,fetch = FetchType.LAZY)
      private Cart cart;
      
      public Quantity() {
          
      }
  }
  </code></pre>
  <p>Below is the code to save the CART into Database</p>
  <pre class="lang-java prettyprint-override"><code>@Override
  public List&lt;Cart&gt; addCartbyUserIdAndProductId (int productId, long userId, int qty, float price) throws Exception {
      try {
  
          // Getting existing cart by user id
  
          List&lt;Cart&gt; cartList = addCartRepo.getCartByuserId(userId);
          List&lt;Product&gt; prdList = new ArrayList&lt;&gt;();
  
          if (cartList.isEmpty()) {
              int i = 1;
              Cart cart = new Cart();
              cart.setUser_id(userId);
              Product product = proServices.getProductsById(productId);
              prdList.add(product);
                   
              List&lt;Quantity&gt; qList = new ArrayList&lt;&gt;(); 
              Quantity quantity = new Quantity();
              quantity.setProductId(product.getId()); 
                   
              quantity.setQuantity(1);
              qList.add(quantity);
              qRepo.save(quantity);
              cart.setProducts(prdList);
              cart.setQuantities(qList);
              cart.setPrice(checkTotalAmountAgainstCart(product.getPrice(), userId));
                   
              addCartRepo.save(cart);
          } else {
              // Fetch cartId for the user id
  
              List&lt;Cart&gt; cList = addCartRepo.getCartByuserId(userId);
              for (Cart curCart : cList) {
                  if (curCart != null) {
                      Product product = proServices.getProductsById(productId);
                      // Calculate Product ID END
  
                      if (!curCart.getProducts().isEmpty()) {
                          List&lt;Product&gt; productList = curCart.getProducts();
  
                          for (Product curPrd : productList) {
                              if (productId == curPrd.getId()) {
  
                                  System.out.println(&quot;matching product found --&gt;&gt;&quot; + productId);
                                  List&lt;Quantity&gt; qList = new ArrayList&lt;&gt;();
                                  if (!curCart.getQuantities().isEmpty()) {
                                          /*
                                           * List&lt;Quantity&gt; curQuantityList = curCart.getQuantities();
                                           * 
                                           * for(Quantity curQuantity : curQuantityList) {
                                           * 
                                           * int preQuantity = curQuantity.getQuantity(); preQuantity = preQuantity + 1;
                                           * Quantity quantity = new Quantity(); quantity.setProductId(curPrd.getId());
                                           * quantity.setQuantity(preQuantity); qList.add(quantity);
                                           * 
                                           * }
                                           */
                                  }
  
                                  curCart.setQuantities(qList);
                                  curCart.setPrice(curCart.getPrice() + curPrd.getPrice());
  
                                  addCartRepo.save(curCart);
                              } else {
                                      curCart.getProducts().add(product);
                                  System.out.println(&quot;curCart &quot; + curCart.getPrice());
                                  double finalPrice = curCart.getPrice() + product.getPrice();
                                  curCart.setPrice(finalPrice);
                                  addCartRepo.save(curCart);
                              }
                          }
                      }
  
                      // Calculate Product ID END
                  }
              }
          }
  </code></pre>
  <p>Hibernate creating Quantity table with following column ID, PRODUCT_ID, QUANTITY, CART_CART_ID. When I store the Quantity using above code the column CART_CART_ID is inserted with null value.</p>