瘋狂

          STANDING ON THE SHOULDERS OF GIANTS
          posts - 481, comments - 486, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
          我這里把代碼貼一下:
            
          Java code  @Override
            
          public
          int countAllSubject() {
              Session session
          = HibernateUtil.getSessionFactory().getCurrentSession();
              session.beginTransaction();
             
          return ((Long) session.createQuery(
                  
          "select count(*) from Post where idParent=0").iterate().next())
                  .intValue();
            }



            
          在我的機器上,用Long作為返回值,運行正常,如果改成Integer,則報如下錯誤
          Java codetype Exception report

          message

          description The server encountered an internal error () that prevented it from fulfilling    this
          request.

          exception

          org.apache.jasper.JasperException: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
              org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:
          522)
              org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
          416)
              org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
          337)
              org.apache.jasper.servlet.JspServlet.service(JspServlet.java:
          266)
              javax.servlet.http.HttpServlet.service(HttpServlet.java:
          803)

          root cause

          java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
              net.java2000.notepad.service.impl.hibernate.PostServiceHibernateImpl.countAllSubject(PostServiceHibernateImpl.java:
          24)
              org.apache.jsp.jsph.index_jsp._jspService(index_jsp.java:
          93)
              org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:
          70)
              javax.servlet.http.HttpServlet.service(HttpServlet.java:
          803)
              org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
          374)
              org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
          337)
              org.apache.jasper.servlet.JspServlet.service(JspServlet.java:
          266)
              javax.servlet.http.HttpServlet.service(HttpServlet.java:
          803)

          note The full stack trace of the root cause is available in the Apache Tomcat
          /6.0.16 logs.


            
            
          但是,有幾個網友卻是正好相反,他們直接運行報
            
          java.lang.ClassCastException: java.lang.Integer  
            
          改成Integer卻正常了。
            
            
          希望大家使用Hibernate比較熟悉的人,確認一下,大家隨意使用 count(*) 然后讀取看看,到底是Integer,還是 Long



          解決方案:
          關于在Hibernate里使用select count(*) 返回值的問題說明
          由于我使用的是Hibernate 3.2版本,經確認,這個版本已經把以前返回 Integer的改成了 Long,
          因為JPA里面的返回值規定是Long, Hibernate為了兼容這個,所以修改了返回值。
            
          如果你從Hibernate 3.0.x/3.1.x升級到最新的3.2版,一定要注意,3.2版的很多sql函數如count(), sum()的唯一返回值已經從Integer變為Long,如果不升級代碼,會得到一個ClassCastException。
            
          這個變化主要是為了兼容JPA,可以在hibernate.org的最新文檔中找到說明。
            
          Hibernate Team也提供了一個與原來兼容的解決方案:
            
             Configuration classicCfg = new Configuration();
             classicCfg.addSqlFunction( "count", new ClassicCountFunction());
             classicCfg.addSqlFunction( "avg", new ClassicAvgFunction());
             classicCfg.addSqlFunction( "sum", new ClassicSumFunction());
             SessionFactory classicSf = classicCfg.buildSessionFactory();
          當然最好統一轉換成Number然后獲取
          主站蜘蛛池模板: 安远县| 三门峡市| 临汾市| 广元市| 信宜市| 张家口市| 沐川县| 闻喜县| 南通市| 阜宁县| 万山特区| 嘉定区| 巴彦县| 舒兰市| 勃利县| 合肥市| 翼城县| 玉门市| 永善县| 萨嘎县| 登封市| 尚志市| 九台市| 景东| 哈密市| 碌曲县| 始兴县| 河北省| 宜春市| 迁西县| 桃江县| 永嘉县| 自贡市| 临汾市| 伊春市| 通许县| 安福县| 遵义市| 九龙坡区| 泸西县| 皮山县|