瘋狂

          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然后獲取
          主站蜘蛛池模板: 斗六市| 台江县| 通城县| 来凤县| 嘉禾县| 怀仁县| 银川市| 文成县| 蓬溪县| 平塘县| 师宗县| 白河县| 军事| 镇坪县| 哈巴河县| 土默特右旗| 和林格尔县| 淄博市| 固镇县| 全南县| 德清县| 乌海市| 阳曲县| 巴楚县| 裕民县| 鱼台县| 东阳市| 安顺市| 沛县| 大方县| 大洼县| 株洲市| 旬阳县| 林口县| 林周县| 皋兰县| 成安县| 安徽省| 襄城县| 桐柏县| 九龙坡区|