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 我心依旧 阅读(6320) 评论(1)  编辑  收藏

FeedBack:
# re: Hibernate 解决SQL count(*)的问题.
2008-10-13 11:03 | 月影销魂
请问org.hibernate.SessionFactory sessionFactory 可以在这个函数体内申明吗?如果不可以的话,那调用这个函数的Action是从哪获取的sessionFactory ?  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: