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.
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "cart_id")
private int cartId;
double price;
int qty;
private long user_id;
@OneToMany(fetch = FetchType.EAGER)
private List<Product> products;
@OneToMany(mappedBy = "cart", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Quantity> 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() {
}
}
Below is the code to save the CART into Database
@Override
public List<Cart> addCartbyUserIdAndProductId (int productId, long userId, int qty, float price) throws Exception {
try {
// Getting existing cart by user id
List<Cart> cartList = addCartRepo.getCartByuserId(userId);
List<Product> prdList = new ArrayList<>();
if (cartList.isEmpty()) {
int i = 1;
Cart cart = new Cart();
cart.setUser_id(userId);
Product product = proServices.getProductsById(productId);
prdList.add(product);
List<Quantity> qList = new ArrayList<>();
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<Cart> 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<Product> productList = curCart.getProducts();
for (Product curPrd : productList) {
if (productId == curPrd.getId()) {
System.out.println("matching product found -->>" + productId);
List<Quantity> qList = new ArrayList<>();
if (!curCart.getQuantities().isEmpty()) {
/*
* List<Quantity> 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("curCart " + curCart.getPrice());
double finalPrice = curCart.getPrice() + product.getPrice();
curCart.setPrice(finalPrice);
addCartRepo.save(curCart);
}
}
}
// Calculate Product ID END
}
}
}
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.