I am developing an E-Commerce website(Spring MVC, java, mySql, Hibernate) for my college project. I have various models like customer,product,orders,custOrderHistory, etc.
I am able to show all customer details(in admin page) and all products for a customer to browse through.
But the problem I am facing is, how do I show the Orders and order history of a customer?
My orders table has orderId, productId, quantity and total price. but to show the product name, I have to use the productId and rerieve the Product name from product table.
I have no clue on how to do it.
Till now, i was using the jstl foreach loop to iterate the products in the productList.jsp page and displaying them in a table. But as i told above, i need to retrieve data from 3-4 tables at once, like productName(from product table), productManuacturer(also from product table), ShippingAddress (from customer table). And I am unsure how to do this.
Edit 1: I asked this question from someone else's computer and didn't have my code with me then. So, I am giving my code here.
Customer.java
package com.site.model; import com.fasterxml.jackson.annotation.JsonIgnore; import org.hibernate.validator.constraints.NotEmpty; import javax.persistence.*; import java.io.Serializable; @Entity public class Customer implements Serializable { private static final long serialVersionUID = -3280023076408333682L; @Id @GeneratedValue private int customerId; @NotEmpty(message = " The customer name must not be blank.") private String customerName; @NotEmpty (message = " The customer email must not be blank.") private String customerEmail; private String customerPhone; @NotEmpty (message = " The username must not be blank.") private String username; @NotEmpty (message = " The password must not be blank.") private String password; private boolean enabled; @OneToOne @JoinColumn(name = "billingAddressId") private BillingAddress billingAddress; @OneToOne @JoinColumn(name = "shippingAddressId") private ShippingAddress shippingAddress; @OneToOne() @JoinColumn(name = "cartId") @JsonIgnore private Cart cart; public int getCustomerId() { return customerId; } public void setCustomerId(int customerId) { this.customerId = customerId; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public BillingAddress getBillingAddress() { return billingAddress; } public void setBillingAddress(BillingAddress billingAddress) { this.billingAddress = billingAddress; } public ShippingAddress getShippingAddress() { return shippingAddress; } public void setShippingAddress(ShippingAddress shippingAddress) { this.shippingAddress = shippingAddress; } public String getCustomerPhone() { return customerPhone; } public void setCustomerPhone(String customerPhone) { this.customerPhone = customerPhone; } public String getCustomerEmail() { return customerEmail; } public void setCustomerEmail(String customerEmail) { this.customerEmail = customerEmail; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public Cart getCart() { return cart; } public void setCart(Cart cart) { this.cart = cart; } }
Cart.java
package com.site.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*; import java.io.Serializable; import java.util.ArrayList; import java.util.List;
@Entity public class Cart implements Serializable{
private static final long serialVersionUID = -2479653100535233857L;
@Id @GeneratedValue private int cartId;
@OneToMany(mappedBy = "cart", cascade= CascadeType.ALL, fetch = FetchType.EAGER) private List cartItems = new ArrayList();
@OneToOne @JoinColumn(name = "customerId") @JsonIgnore private Customer customer;
private double grandTotal;
public int getCartId() { return cartId; }
public void setCartId(int cartId) { this.cartId = cartId; }
public double getGrandTotal() { return grandTotal; }
public void setGrandTotal(double grandTotal) { this.grandTotal = grandTotal; }
public List getCartItems() { return cartItems; }
public void setCartItems(List cartItems) { this.cartItems = cartItems; }
public Customer getCustomer() { return customer; }
public void setCustomer(Customer customer) { this.customer = customer; }
}
CartItem.java
package com.site.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*; import java.io.Serializable; import java.util.List;
@Entity public class CartItem implements Serializable{
private static final long serialVersionUID = -904360230041854157L; @Id @GeneratedValue private int cartItemId; @ManyToOne @JoinColumn(name="cartId") @JsonIgnore private Cart cart; @ManyToOne @JoinColumn(name = "productId") private Product product; private int quantity; private double totalPrice; @ManyToMany(cascade=CascadeType.ALL, mappedBy="cartItems") @JsonIgnore private List<OrderHistory> orderHistoryList; public int getCartItemId() { return cartItemId; } public void setCartItemId(int cartItemId) { this.cartItemId = cartItemId; } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public double getTotalPrice() { return totalPrice; } public void setTotalPrice(double totalPrice) { this.totalPrice = totalPrice; } public Cart getCart() { return cart; } public void setCart(Cart cart) { this.cart = cart; } public List<OrderHistory> getOrderHistoryList() { return orderHistoryList; } public void setOrderHistoryList(List<OrderHistory> orderHistoryList) { this.orderHistoryList = orderHistoryList; } }
CustomerOrder.java
package com.site.model;
import javax.persistence.*; import java.io.Serializable;
@Entity public class CustomerOrder implements Serializable {
private static final long serialVersionUID = -3608286390950243118L; @Id @GeneratedValue private int customerOrderId; @OneToOne @JoinColumn(name = "cartId") private Cart cart; @OneToOne @JoinColumn(name = "customerId") private Customer customer; @OneToOne @JoinColumn(name = "billingAddressId") private BillingAddress billingAddress; @OneToOne @JoinColumn(name = "shippingAddressId") private ShippingAddress shippingAddress; public int getCustomerOrderId() { return customerOrderId; } public void setCustomerOrderId(int customerOrderId) { this.customerOrderId = customerOrderId; } public Cart getCart() { return cart; } public void setCart(Cart cart) { this.cart = cart; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public BillingAddress getBillingAddress() { return billingAddress; } public void setBillingAddress(BillingAddress billingAddress) { this.billingAddress = billingAddress; } public ShippingAddress getShippingAddress() { return shippingAddress; } public void setShippingAddress(ShippingAddress shippingAddress) { this.shippingAddress = shippingAddress; } }
OrderHistory.java
package com.site.model;
import javax.persistence.*; import java.io.Serializable; import java.util.ArrayList; import java.util.List;
@Entity public class OrderHistory implements Serializable {
private static final long serialVersionUID = 1083533250613139445L; @Id @GeneratedValue private int orderHistoryId; private int customerId; private String customerName; private int customerOrderId; private int cartId; @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "cartItem_orderHistory", joinColumns = @JoinColumn(name = "orderHistoryId"), inverseJoinColumns = @JoinColumn (name = "cartItemId")) private List<CartItem> cartItems = new ArrayList<CartItem>(); private double grandTotal; private String billingAddress; private String shippingAddress; public int getOrderHistoryId() { return orderHistoryId; } public void setOrderHistoryId(int orderHistoryId) { this.orderHistoryId = orderHistoryId; } public int getCustomerId() { return customerId; } public void setCustomerId(int customerId) { this.customerId = customerId; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public int getCustomerOrderId() { return customerOrderId; } public void setCustomerOrderId(int customerOrderId) { this.customerOrderId = customerOrderId; } public int getCartId() { return cartId; } public void setCartId(int cartId) { this.cartId = cartId; } public List<CartItem> getCartItems() { return cartItems; } public void setCartItems(List<CartItem> cartItems) { this.cartItems = cartItems; } public double getGrandTotal() { return grandTotal; } public void setGrandTotal(double grandTotal) { this.grandTotal = grandTotal; } public String getBillingAddress() { return billingAddress; } public void setBillingAddress(String billingAddress) { this.billingAddress = billingAddress; } public String getShippingAddress() { return shippingAddress; } public void setShippingAddress(String shippingAddress) { this.shippingAddress = shippingAddress; } }
edit 2: how do I store the data in the ustomerorder table and orderhistory table, and also, how do i show it on a jsp page?