here is my parent class named as Shopping_cart.java
it contains shopping_cart_id,product_id,user_id,product_name,price.
我需要像1 ------------- item1这样的输出 1 ------------- item2 1 -------------- item3 1 -------------- item4 2 ------------- item1 2 ------------- item2 2 -------------- item3 2 -------------- item4等。
我在数组列表shopping_cart中添加了多个产品详细信息,并将其存储在一个名为list的会话中。我在控制器方法“ proceedToPay”方法中获取了多个产品详细信息会话列表 在proceedToPay方法中,我首先将值添加到shopping_cart,然后在产品表中更新,然后尝试将值插入到shopping_cart_items。但是我的问题是,我使用一对多关系将值插入到shopping_cart_items。,但不是显示逻辑
package com.jwt.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.springframework.stereotype.Component;
@Entity
@Table(name = "Shopping_Cart")
@Component
public class Shopping_Cart implements Serializable {
private static final long serialVersionUID = -3465813074586302847L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int shopping_cart_id;
@Column
private int product_id;
@Column(name = "user_id", unique = true)
private int user_id;
@Column(name = "product_name")
private String product_name;
@Column(name = "price", unique = true)
private String price;
//mappedBy table name of parent class
@OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL, mappedBy = "shopping_cart", orphanRemoval = true)
private transient List<Shopping_Cart_Items> items;
public Shopping_Cart()
{
items = new ArrayList<Shopping_Cart_Items>();
}
/*
public void addItem(Shopping_Cart_Items item) {
if (item != null)
items.add(item);
} */
public Shopping_Cart(int product_id, int user_id,String product_name, String price) {
super();
this.product_id = product_id;
this.user_id = user_id;
this.product_name= product_name;
this.price = price;
}
public Shopping_Cart( int shopping_cart_id,int product_id, int user_id, String product_name, String price) {
super();
this.shopping_cart_id=shopping_cart_id;
this.product_id = product_id;
this.user_id = user_id;
this.product_name= product_name;
this.price = price;
}
public Shopping_Cart(int shopping_cart_id2, List<Shopping_Cart_Items> item_list) {
// TODO Auto-generated constructor stub
}
public int getShopping_cart_id() {
return shopping_cart_id;
}
public void setShopping_cart_id(int shopping_cart_id) {
this.shopping_cart_id = shopping_cart_id;
}
public int getProduct_id() {
return product_id;
}
public void setProduct_id(int product_id) {
this.product_id = product_id;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public List<Shopping_Cart_Items> getItems() {
return items;
}
public void setItems(List<Shopping_Cart_Items> items) {
this.items = items;
}
/*
public Set<Shopping_Cart_Items> getItems() {
return items;
}
public void setItems(Set<Shopping_Cart_Items> items) {
this.items = items;
}*/
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
here is my child class named as shopping_cart_items.java
Shopping_Cart_Items contais shopping_cart_items_id,shopping_cart_id,product_id.
here is my child class named as shopping_cart_items.java
Shopping_Cart_Items contais shopping_cart_items_id,shopping_cart_id,product_id.
here is my child class named as shopping_cart_items.java
Shopping_Cart_Items contais shopping_cart_items_id,shopping_cart_id,product_id.
package com.jwt.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.springframework.stereotype.Component;
@Entity
@Table(name = "Shopping_Cart_Items")
@Component
public class Shopping_Cart_Items implements Serializable {
private static final long serialVersionUID = -3465813074586302847L;
@Id
@Column
private int shopping_cart_items_id;
@Column
private int shopping_cart_id;
@Column
private int product_id;
@ManyToOne
@JoinColumn(name = "shopping_ID")
private Shopping_Cart shopping_cart;
public Shopping_Cart_Items()
{
}
public Shopping_Cart_Items(int shopping_cart_id, int product_id) {
super();
this.shopping_cart_id = shopping_cart_id;
this.product_id = product_id;
}
public Shopping_Cart_Items(Shopping_Cart shopping_cart) {
super();
this.shopping_cart = shopping_cart;
}
public Shopping_Cart_Items(int shopping_cart_id, Shopping_Cart shopping_cart) {
super();
this.shopping_cart_id = shopping_cart_id;
this.shopping_cart = shopping_cart;
}
public int getShopping_cart_items_id() {
return shopping_cart_items_id;
}
public void setShopping_cart_items_id(int shopping_cart_items_id) {
this.shopping_cart_items_id = shopping_cart_items_id;
}
public int getShopping_cart_id() {
return shopping_cart_id;
}
public void setShopping_cart_id(int shopping_cart_id) {
this.shopping_cart_id = shopping_cart_id;
}
public int getProduct_id() {
return product_id;
}
public void setProduct_id(int product_id) {
this.product_id = product_id;
}
public Shopping_Cart getShopping_cart() {
return shopping_cart;
}
public void setShopping_cart(Shopping_Cart shopping_cart) {
this.shopping_cart = shopping_cart;
}
}
here is my table schema
i have two tables 1 is shopping_cart
and the 2nd one is shopping_cart_items
here is my table schema
i have two tables 1 is shopping_cart
and the 2nd one is shopping_cart_items
here is my table schema
i have two tables 1 is shopping_cart
and the 2nd one is shopping_cart_items
//3.creating shopping_cart table
create table shopping_cart(
shopping_cart_id int NOT NULL AUTO_INCREMENT,
product_id INTEGER(20),
user_id INTEGER(20),
product_name varchar(20),
price varchar(20),
primary key(shopping_cart_id),
CUSTOMER_ID INT references customer (user_id),
PROD_ID INT references product(product_id));
这是shopping_cart_items表的详细信息 这是shopping_cart_items表的详细信息 这是shopping_cart_items表的详细信息 这是shopping_cart_items表的详细信息 创建表shopping_cart_items( shopping_cart_items_id int NOT NULL AUTO_INCREMENT, shopping_cart_id int, Product_id int, 主键(shopping_cart_items_id), shopping_ID INT引用Shopping_Cart(shopping_cart_id));
here is my controller method named as proceedToPay
proceedToPay method
here is my controller method named as proceedToPay
proceedToPay method
here is my controller method named as proceedToPay
proceedToPay method
//proceed to pay adding multiple list values to the database
@RequestMapping(value = "/proceedToPay", method = RequestMethod.GET)
public ModelAndView proceedToPay(ModelAndView model,ModelMap models,
HttpServletRequest req,
HttpServletResponse res,
HttpSession session,
@ModelAttribute Product product,
@ModelAttribute Shopping_Cart shopping_cart)
{
//adding multiple products to the database
session=req.getSession();
List<Shopping_Cart> list= (List<Shopping_Cart>) session.getAttribute("list");
List<Product> productlist= (List<Product>) session.getAttribute("productlist");
int product_id = 0;
int users_id = 0;
String product_name=null;
String price = null;
int shopping_cart_id=0;
for(Shopping_Cart cart : list){
product_id = cart.getProduct_id();
users_id = cart.getUser_id();
product_name = cart.getProduct_name();
price = cart.getPrice();
//adding product_id, user_id, product_name and price to the shopping_cart table
shopping_cartService.addShopping_Cart(product_id,users_id,product_name,price);
//click on addtocart link it will reducing the number of columns from product table available_unit column
productService.updateProduct(product_id);
//inserting shopping_cart_item values here
//--------------------------------------
//here is the problem of inserting shopping_cart_id with foreign key values, now here i
//initialized int shopping_cart_id='0';
shopping_cart_itemsService.addShopping_Cart_Items(shopping_cart_id,product_id);
}
here are my service classes of shopping_cart
here are my service classes of shopping_cart
here are my service classes of shopping_cart
here are my service classes of shopping_cart
package com.jwt.service;
import java.util.Date;
import java.util.List;
import com.jwt.model.Shopping_Cart;
public interface Shopping_CartService {
public void addShopping_Cart(int product_id,int user_id,String product_name,String price);
public List<Shopping_Cart> getAllShopping_Carts();
// public boolean updateShopping_Cart(int user_id);
}
here my shopping_cartServiceImpl class
here my shopping_cartServiceImpl class
here my shopping_cartServiceImpl class
here my shopping_cartServiceImpl class
package com.jwt.service;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jwt.dao.Shopping_CartDAO;
import com.jwt.model.Shopping_Cart;
@Service("Shopping_CartService")
@Transactional
public class Shopping_CartServiceImpl implements Shopping_CartService {
@Autowired
private Shopping_CartDAO shopping_cartDAO;
// @Override
@Transactional
public void addShopping_Cart(int product_id,int user_id,String product_name, String price) {
shopping_cartDAO.addShopping_Cart(product_id,user_id,product_name,price);
}
// @Override
@Transactional
public List<Shopping_Cart> getAllShopping_Carts() {
return shopping_cartDAO.getAllShopping_Carts();
}
/*
public boolean updateShopping_Cart(int user_id){
return shopping_cartDAO.updateShopping_Cart(user_id);
} */
}
here is shopping_cartDAO class
here is shopping_cartDAO class
here is shopping_cartDAO class
here is shopping_cartDAO class
package com.jwt.dao;
import java.util.Date;
import java.util.List;
import com.jwt.model.Shopping_Cart;
public interface Shopping_CartDAO {
public void addShopping_Cart(int product_id,int user_id,String product_name, String price);
public List<Shopping_Cart> getAllShopping_Carts();
// public boolean updateShopping_Cart(int user_id);
}
here is my shopping_cart_itemsservice class
here is my shopping_cart_itemsservice class
here is my shopping_cart_itemsservice class
here is my shopping_cart_itemsservice class
package com.jwt.service;
import java.util.Date;
import java.util.List;
import com.jwt.model.Shopping_Cart;
import com.jwt.model.Shopping_Cart_Items;
public interface Shopping_Cart_ItemsService {
public void addShopping_Cart_Items(int shopping_cart_id,int product_id);
public List<Shopping_Cart_Items> getAllShopping_Cart_Itemss();
}
here is shopping_cart_itemsserviceimpl class
here is shopping_cart_itemsserviceimpl class
here is shopping_cart_itemsserviceimpl class
here is shopping_cart_itemsserviceimpl class
package com.jwt.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jwt.dao.Shopping_Cart_ItemsDAO;
import com.jwt.model.Shopping_Cart;
import com.jwt.model.Shopping_Cart_Items;
@Service("Shopping_Cart_ItemsService")
@Transactional
public class Shopping_Cart_ItemsServiceImpl implements Shopping_Cart_ItemsService {
@Autowired
private Shopping_Cart_ItemsDAO shopping_cart_itemsDAO;
// @Override
@Transactional
public void addShopping_Cart_Items(int shopping_cart_id,int product_id) {
shopping_cart_itemsDAO.addShopping_Cart_Items(shopping_cart_id,product_id);
}
// @Override
@Transactional
public List<Shopping_Cart_Items> getAllShopping_Cart_Itemss() {
return shopping_cart_itemsDAO.getAllShopping_Cart_Itemss();
}
}
here is shopping_cartDAO class
here is shopping_cartDAO class
here is shopping_cartDAO class
package com.jwt.dao;
import java.util.List;
import com.jwt.model.Shopping_Cart;
import com.jwt.model.Shopping_Cart_Items;
public interface Shopping_Cart_ItemsDAO {
public void addShopping_Cart_Items(int shopping_cart_id,int product_id);
public List<Shopping_Cart_Items> getAllShopping_Cart_Itemss();
}
here is shopping_cart_itemsDAOImpl class
here is shopping_cart_itemsDAOImpl class
here is shopping_cart_itemsDAOImpl class
here is shopping_cart_itemsDAOImpl class
package com.jwt.dao;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.jwt.model.Customer;
import com.jwt.model.Shopping_Cart;
import com.jwt.model.Shopping_Cart_Items;
@Repository("Shopping_Cart_ItemsDAO")
public class Shopping_Cart_ItemsDAOImpl implements Shopping_Cart_ItemsDAO {
@Autowired
private SessionFactory sessionFactory;
public void addShopping_Cart_Items(int shopping_cart_id,int product_id) {
System.out.println("In Check shoppingcartitems");
Session session = sessionFactory.openSession();
Shopping_Cart_Items item = new Shopping_Cart_Items();
List<Shopping_Cart_Items> item_list = new ArrayList<Shopping_Cart_Items>();
item_list.add(item);
Shopping_Cart shopping_cart = new Shopping_Cart(shopping_cart_id,item_list);
item.setShopping_cart(shopping_cart);
shopping_cart.setItems(item_list);
item.setShopping_cart_id(shopping_cart_id);
item.setProduct_id(product_id);
session.save(item);
session.save(shopping_cart);
session.close();
}
@SuppressWarnings("unchecked")
public List<Shopping_Cart_Items> getAllShopping_Cart_Itemss() {
return sessionFactory.getCurrentSession().createQuery("from Shopping_Cart_Items")
.list();
}
}