首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

使用hibernate.sessionfactory时插入多条语句时,直插入了最后一天语句,前面的都被覆盖了。咋回事

2012-05-12 
使用hibernate.sessionfactory时插入多条语句时,直插入了最后一天语句,前面的都被覆盖了。怎么回事?代码如

使用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_file);
//// 获取去连接工厂
//SessionFactory sessionFactory = configuration.buildSessionFactory();
//
//Session session = null;
//try {
//// 获取连接
//session = sessionFactory.openSession();


//session.beginTransaction();// 获取事物
//// 插入信息
//session.save(inventory);
//session.getTransaction().commit();
//} catch (HibernateException e) {
//e.printStackTrace();
//session.getTransaction().rollback();
//} finally {
//if (session != null) {
//session.close();
//}
//}
HibernateSessionFactory.getSession().save(inventory);



}

}



XMl:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<hibernate-mapping>
<class name="com.iprecare.entity.Inventory" table="Inventory"
schema="dbo" catalog="SalesiPreCare">
<id name="inventoryId" type="java.lang.Integer">
<column name="InventoryId" />
<generator class="identity"></generator>
</id>

<many-to-one name="product" class="com.iprecare.entity.Product"
lazy="false" fetch="join" insert="false" update="true">
<column name="ProductId" />
</many-to-one>

<many-to-one name="users" class="com.iprecare.entity.Users"
lazy="false" update="true" fetch="join" insert="false"
not-null="true">
<column name="UserId" />
</many-to-one>

<property name="inventoryCount" type="java.lang.Integer">
<column name="InventoryCount" />
</property>
<property name="inventoryPrice" type="java.lang.Double">
<column name="InventoryPrice" scale="4" />
</property>
<property name="inventoryStatus" type="java.lang.String">
<column name="InventoryStatus" length="10" />
</property>
</class>
</hibernate-mapping>


因为使用的是同一个session,为了共享数据,但是 如果执行插入语句的话之后执行session.clear()方法之后就无法共享数据,导致级联查询失败。

在不使用二级缓存的情况下,如何解决 同一个session插入多条语句,之前插入语句不会被覆盖。


请各位大侠帮帮忙!




[解决办法]
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;

第三行i.userId=:userId 怎么多了个:,这是怎么回事?
[解决办法]

探讨

HibernateSessionFactory.getSession().clear();
Inventory invent = InventoryFactory.getInventory();
String hql = "from Inventory i where i.userId=:userId";
invent = (Inventory) HibernateSessionFacto……

热点排行