org.unsaved transient instance - save the transient instance before flushing: bug解决方案

最近开发和学习过程中,遇到很多零碎的知识点,在此简单地记录下:

1.遇如下bug:

org.unsaved transient instance - save the transient instance before flushing: org.blog.blog.domain.HighStudent

解释:意思是说hibernate从数据库中查询出来的一个对象HighSchool(为持态,我给它的事物定义为readOnly,只有只读权限),然后又给它的属性或者它属性的属性(highStudents.comment.cxpy)进行set值,这样hibernate是不允许的。因为你对一个事物为只读状态的对象进行赋值,显然是不允许,除非你主动调用saveOrUpate

方法。serice方法事物是readOnly,dao中就查询出来的对象不允许修改属性。

解决方法:将查询出来的对像使用session.evict()方法转换为游离态,此时就可以查询成功。代码如下:

 

@Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<PrimaryStudents> getPrimaryStudentsList(String[] stuids)
            throws StuBaseDataException {
        List<PrimaryStudents> list=new ArrayList<PrimaryStudents>();
         String  stu=stuids[0];
         String [] studentids=stu.split(",");
         System.out.println(studentids.length);
        try{
           if(studentids.length>0){
             for(int i=0;i<studentids.length ;i++){
                  PrimaryStudents primaryStudents=null;
                  primaryStudents=this.primaryStudentsDao.queryPrimaryStudentsBystuid(studentids[i]);
                  if(primaryStudents==null){
                      throw new StuBaseDataException("该批学生中存在学生信息不存在的学生,请刷新之后再进行该操作");
                  }
                  list.add(primaryStudents);
             }
           }else{
               throw new StuBaseDataException("请选择一位学生");
           }
        }catch (Exception e) {
            e.printStackTrace();
            throw new StuBaseDataException("查询失败!!!",e);
        }
        return list;
    }

 

 

 

 

 

    @Override
    public PrimaryStudents queryPrimaryStudentsBystuid(String stuid) {
        List<PrimaryStudents> list= null;
        Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
        String sql="From PrimaryStudents ps where ps.id in("+stuid+")";
        Query query = session.createQuery(sql);
        list = query.list();
        if (list != null && list.size() > 0)
        {
            /**
             * 在做学籍卡批量打印的时候,显示问题。
             * 评语不足6个的时候,构造几个空的,显示到页面上去。
             * 强制转换为游离态
             */
            session.evict(list.get(0));
          return (PrimaryStudents)list.get(0);
        }
         return null;
    }

 

posted @ 2014-08-23 08:19  程序员张越  阅读(1471)  评论(0编辑  收藏  举报