使用hibernate.sessionfactory时插入多条语句时,直插入了最后一天语句,前面的都被覆盖了。怎么回事?
代码如下:
package com.iprecare.action.inventory;
import java.util.ArrayList;
import java.util.List;
import com.iprecare.action.BaseAction;
import com.iprecare.dao.InventoryDao;
import com.iprecare.dao.OrderDao;
import com.iprecare.entity.Inventory;
import com.iprecare.entity.Item;
import com.iprecare.entity.Order;
import com.iprecare.entity.Users;
import com.iprecare.factory.DAOFactory;
import com.iprecare.factory.InventoryFactory;
public class addInventoryAction extends BaseAction {
private Integer orderId;
public String execute() throws Exception {
List<Inventory> inventorylist = new ArrayList<Inventory>();
OrderDao orderdao = DAOFactory.getOrderDao();
Order order = orderdao.getOrder(orderId);
InventoryDao inventoryDao = DAOFactory.getInventoryDao();
Inventory inventory = InventoryFactory.getInventory();
Users user = order.getReceiveAddress().getUsers();
List<Item> items = new ArrayList<Item>();
List<Item> items2 = new ArrayList<Item>();
items = order.getItems();
for (Item item : items) {
if (item != null && !item.equals(null)) {
items2.add(item);
}
}
for (Item item : items2) {
inventory.setUsers(user);
inventory.setProduct(item.getProduct());
inventory.setInventoryCount(item.getProductNum());
inventory.setInventoryPrice(item.getMemberPrice()
* item.getProductNum());
inventorylist.add(inventory);
inventoryDao.addInventory(inventory);//执行循环插入
}
return "success";
}
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
}
Dao:
package com.iprecare.dao.impl;
import java.util.List;
import
org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.iprecare.dao.InventoryDao;
import com.iprecare.entity.Inventory;
import com.iprecare.factory.InventoryFactory;
import com.iprecare.utils.HibernateSessionFactory;
public class InventoryDaoimpl implements InventoryDao {
@Override
public Inventory getinventByUserId(Integer userId) throws Exception {
// TODO Auto-generated method stub
HibernateSessionFactory.getSession().clear();
Inventory invent = InventoryFactory.getInventory();
String hql = "from Inventory i where i.userId=:userId";
invent = (Inventory) HibernateSessionFactory.getSession()
.createQuery(hql).setParameter("userId", userId).uniqueResult();
HibernateSessionFactory.getSession().flush();
return invent;
}
@Override
public void addInventoryList(List<Inventory> inventorylist)
throws Exception {
// TODO Auto-generated method stub
for (Inventory inventory : inventorylist) {
HibernateSessionFactory.getSession().getTransaction().begin();
HibernateSessionFactory.getSession().beginTransaction();
HibernateSessionFactory.getSession().save(inventory);
HibernateSessionFactory.getSession().getTransaction().commit();
HibernateSessionFactory.getSession().close();
}
}
public void addInventory(Inventory inventory) throws Exception {
// String hibernate_config_file = "/hibernate.cfg.xml";
// // 读取hibernate配置文件
// Configuration configuration = new Configuration()
// .configure(hibernate_config_fi