posts - 70,comments - 408,trackbacks - 0

          // 用于hibernate 2.x
          protected String getCountSql(String originalHql, net.sf.hibernate.SessionFactory sessionFactory) throws Exception {
          QueryTranslator translator = new QueryTranslator(originalHql);

          translator.compile((net.sf.hibernate.engine.SessionFactoryImplementor)sessionFactory, Collections.EMPTY_MAP, false);

          return "select count(*) from (" + translator.getSQLString() + ") tmp_count_t";
          }
          // 用于hibernate 3.x
          protected String getCountSql(String originalHql, org.hibernate.SessionFactory sessionFactory) throws Exception {
          QueryTranslatorImpl queryTranslator = new QueryTranslatorImpl(originalHql, originalHql,
          Collections.EMPTY_MAP, (org.hibernate.engine.SessionFactoryImplementor)sessionFactory);

          queryTranslator.compile(Collections.EMPTY_MAP, false);

          return "select count(*) from (" + queryTranslator.getSQLString() + ") tmp_count_t";
          }

          String strHQL = "Select count(*) From tb_po";
          Long count = (Long)session.createQuery(strHQL).uniqueResult();

          posted on 2007-06-05 11:28 我心依舊 閱讀(6593) 評(píng)論(1)  編輯  收藏

          FeedBack:
          # re: Hibernate 解決SQL count(*)的問題.
          2008-10-13 11:03 | 月影銷魂
          請(qǐng)問org.hibernate.SessionFactory sessionFactory 可以在這個(gè)函數(shù)體內(nèi)申明嗎?如果不可以的話,那調(diào)用這個(gè)函數(shù)的Action是從哪獲取的sessionFactory ?  回復(fù)  更多評(píng)論
            

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 沙田区| 遂昌县| 大足县| 神池县| 合肥市| 开远市| 青州市| 龙山县| 晋江市| 大邑县| 绥中县| 象州县| 库车县| 朝阳市| 永安市| 辽宁省| 黑水县| 贡嘎县| 陕西省| 彭州市| 丰顺县| 石门县| 衡山县| 万安县| 东港市| 航空| 东莞市| 南靖县| 宁陕县| 衡阳县| 阿克陶县| 巴里| 澄迈县| 宁安市| 永宁县| 靖宇县| 开平市| 石家庄市| 阳朔县| 澄城县| 阳山县|