求助hibernate :failed to lazily initialize a collect
这是Bill.hbm.xml:
测试代码:
BillDAO.findBill()
我在网上查到是hibernate懒加载的问题,其中一种解决方法是把lazy设置成false,我这么做了,但是为什么还会发生相同的错误呢,我是在netbeans下运行
<class name="entity.Bill" table="Bill">
<id name="id" column="oid">
<generator class="increment" />
</id>
<property name="tableNum" column="table_num" />
<property name="consumption" column="consumption" />
<property name="isFinished" column="is_finished" />
<property name="createDate" column="create_date" />
<set name="itemSet" cascade="all" lazy="false">
<key column="Bill_id" />
<one-to-many class="entity.Item" />
</set>
</class>测试代码:
Dish dish1 = DishDAO.findDishByName("apple");
Bill bill = BillDAO.findBill("B");
Item item = ItemDAO.createItem(new Item(1,dish1));
bill.getItemSet().add(item);BillDAO.findBill()
public static Bill findBill(String tableNum) throws Exception {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Bill bill;
Query query = session.createQuery("from Bill bill where bill.tableNum=:tableNum and bill.isFinished=false");
query.setParameter("tableNum", tableNum);
List list = query.list();
Iterator iter = list.iterator();
if(iter.hasNext()) {
bill = (Bill) iter.next();
}else {
tx.commit();
session.close();
throw new Exception("bill not found");
}
tx.commit();
session.close();
return bill;
}我在网上查到是hibernate懒加载的问题,其中一种解决方法是把lazy设置成false,我这么做了,但是为什么还会发生相同的错误呢,我是在netbeans下运行
...全文
相关推荐
hibernate的slf4j-nop包
本文介绍了如何解决 Hibernate 中出现的 SLF4J: Failed to load class 'org.slf4j.impl.StaticLoggerBinder' 错误。方法是将 slf
SSH整合中 hibernate托管给Spring得到SessionFactory
这通常是传统的方式,如下:```javaConfiguration configuration = new Configuration().configure("/WEB-INF/hibernate.cfg.xml
hibernate错误解决方案
of bean failed; nested exception is org.hibernate.PropertyNotFoundException: Could not find a getter
Could not roll back Hibernate transaction.doc
: JDBC rollback failed这表明Hibernate事务回滚操作失败,导致事务不能正确回滚。
使用Hibernate一些常见错误解决办法
**当前会话未绑定到上下文**: 当遇到`no current session bound to current context`错误时,通常是因为事务管理方式不正确。


Web 开发
