1. hibernate need an active transaction
2. you should roll back the transaction immediately after any db error!
3. hibernate doesn't support save point..so, in you case.. you can only use begintran & commit inside your for clause and use try..catch to make sure that one failed operation won't stop your for cluase.

解决方案 »

  1.   

    thanks,修改后的代码开启事务后不再报错了:
    //根据ID取得子类树
        List subCategorys=new ArrayList();
        public List getSubCategorysTree(int ID){
            List list = new ArrayList();
            try {
                session = HibernateUtil.getSession();
                tx = session.beginTransaction();
                String hql = "from Category where fatherID=" + ID;
                list = session.createQuery(hql).list();
                tx.commit();
            } catch (Exception e) {
                tx.rollback();
                log.error("生成ID为 " + ID + " 的子类树失败!", e);
            } finally {
                HibernateUtil.closeSession();
            }
            try {
                for (int i = 0; i < list.size(); i++) {
                    Category categoryPO = (Category) list.get(i);
                    CategoryVO categoryVO = new CategoryVO();
                    BeanUtils.copyProperties(categoryVO, categoryPO);
                    subCategorys.add(categoryVO);
                    this.getSubCategorysTree(categoryPO.getID());
                }
            } catch (Exception e) {
                log.error("生成ID为 " + ID + " 的子类树失败!", e);
            }
            return subCategorys;      
        }