org.hibernate.HibernateException:非法尝试将集合与两个打开的会话相关联

时间:2014-06-06 11:30:17

标签: java hibernate

首先,我使用jsfprimeface创建一个表单并填写表单并成功将数据保存在数据库中以后我创建了一个表从数据库中获取数据到我们的表后面我已经做了行是可编辑的,我已创建方法是attachDirty方法。要在单击行的铅笔图标时保存或更新数据,则会发生例外"Illegal attempt to associate a collection with two open sessions"

我的DAO课程代码在这里:

public void attachDirty(Facility instance)
   {
       System.out.println("attachdirty of FacilityHome class");
       log.debug("attaching dirty Facility instance");
       Session session=null;
       try
       {
           System.out.println("TRY BLOCK");
           session=sessionFactory.openSession();
           ((SessionImpl) getSession()).connection().setAutoCommit(false);
           Transaction tx=session.beginTransaction();
           session.saveOrUpdate(instance);
           tx.commit();
           log.debug("attach successful");
             System.out.println("exit from try");
           }
           catch (SQLException re) {
              System.out.println("Sqlexception");
              log.error("attach failed", re);
           }
       catch(RuntimeException re)
       {
           System.out.println("runt time exception");
           log.error("attach failed", re);
           throw re; 
       }
       finally
       {
           if(session!=null)
           {
               session.flush();
               session.close();
           }
       }
   }

0 个答案:

没有答案