posts - 40,  comments - 4,  trackbacks - 0

          使用Hibernate的sum函數(shù)進(jìn)行數(shù)據(jù)的統(tǒng)計(jì)時(shí),出現(xiàn)一個(gè)錯(cuò)誤:

           

          String sql = "select SUM(nf.fee) from CFee as nf where   nf.adminAccount='testaccount' ";   
          public long getListSqlCountsLong(String sql) {   
                  beginTransaction();   
                  List li 
          = getSession().createQuery(sql).list();   
                  
          if (li == null || li.isEmpty()) {   
                      
          return 0;   
                  }
           else {                        return ((Integer) li.get(0)).longValue();   
                  }
             
              }
           
          這樣使用報(bào)null錯(cuò)誤.
          List的size明明等于1,但li.get(0)還是為空.(數(shù)據(jù)庫中查詢的賬號(hào)sum本來就為null??可能是.)
          解決方法:
          String sql = "select SUM(nf.fee) from CFee as nf where   nf.adminAccount='testaccount' ";   
          public long getListSqlCountsLong(String sql) {   
                  beginTransaction();   
                  List li 
          = getSession().createQuery(sql).list();   
                  
          if (li == null || li.isEmpty()) {   
                      
          return 0;   
                  }
           else {       
                          
          if (li.get(0== null{   
                          
          return 0;   
                      }
             
                      
          return ((Integer) li.get(0)).longValue();   
                  }
             
              }
            
          解決方法很簡(jiǎn)單,就是增加一個(gè)判斷就可以了,如果li.get(0)為空,則返回0,不為空,返回值.
          posted on 2008-05-09 13:17 larryjava 閱讀(4026) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 响水县| 闽侯县| 诏安县| 新郑市| 泾源县| 屏东县| 宣城市| 宜黄县| 正安县| 新平| 莲花县| 饶平县| 阳春市| 荆门市| 自贡市| 云梦县| 乳山市| 丹阳市| 维西| 犍为县| 高唐县| 鄂托克前旗| 松桃| 隆安县| 巴里| 都安| 嘉禾县| 杭州市| 巴南区| 讷河市| 永州市| 文化| 南昌县| 大方县| 饶阳县| 仙桃市| 万盛区| 察隅县| 新营市| 满城县| 嵊州市|