我作统计功能,运行结果似乎没问题,但是显示不出来结果。麻烦大家帮看看。各层代码如下:
这个功能希望实现对每个国家召开的会议次数进行统计。在sqlserver里的查询语句为:
select conferencecountry,count(conferencecountry) as countrycount from T_conference where conferencecountry is not null group by conferencecountry order by countrycount desc
public String stabycountry() throws Exception {
  String queryName = "conferencecountry"
  String orderby = "count(" + queryName + ")";
  String groupby = queryName;
  String sortby = "desc";
  availableItems = tconferenceService.staCountry(TConference.class, "id", queryName, groupby, orderby, sortby);
  return SUCCESS;
}
GenericDAOImpl 层
public List<T> staCountry(Class<T> entityClass, ID id, String fieldname, String groupby, String orderby, String sortby){
  final String entityname = entityClass.getName();
  final String queryName = fieldname;
  final String group = groupby;
  final String order = orderby;
  final String sort = sortby;
  return this.getHibernateTemplate().executeFind(new HibernateCallback() {
    public List doInHibernate(Session session) throws HibernateException, SQLException {
    Query query = session.createQuery("select " + queryName + ", count(" + queryName + ") as countrycount from " + entityname + " where " + queryName + " is not null group by " + group + " order by " + order + " " + sort);
   System.out.println(query);
   return query.list();
}
}); 
}
JSP层 <s:iterator value="availableItems" status="tConference">
<tr>
<td align="center"><s:property value="#tConference.count" /></td>    
    <!-- 显示国家名称 -->
  <td align="center"><s:property value="conferencecountry" /></td>

<!-- 显示举办会议数量 --> 
<td align="center"><s:property value="countrycount" /></td>

<!-- 查看详细信息-->
<td align="center"><a href="ListStaCountryAction.action?country=<s:property value="conferencecountry" />"><font color="#C2C2C2">查看详细</font></a></td> </tr>
</s:iterator>
错误现象是  国家名称和会议数量没有显示出来。我打印了系统执行的query和hql,内容应该没错:
QueryImpl(select conferencecountry, count(conferencecountry) as countrycount from meetings.ssh.model.TConference where conferencecountry is not null group by conferencecountry order by count(conferencecountry) desc)Hibernate: select tconferenc0_.conferencecountry as col_0_0_, count(tconferenc0_.conferencecountry) as col_1_0_ from meeting.dbo.T_conference tconferenc0_ where tconferenc0_.conferencecountry is not null group by tconferenc0_.conferencecountry order by count(tconferenc0_.conferencecountry) desc谢谢大家,费心了!

解决方案 »

  1.   

    其实就是HQL怎么转换成SQL的问题。
      

  2.   


    public List<T> statistic(Class<T> entityClass, ID id, String fieldname, String groupby, String orderby, String sortby){
    final String entityname = entityClass.getName();
    final String queryName = fieldname;
    final String group = groupby;
    final String order = orderby;
    final String sort = sortby;
    return this.getHibernateTemplate().executeFind(new HibernateCallback() {
    public List doInHibernate(Session session) throws HibernateException, SQLException {
    Query query = session.createQuery("select " + queryName + ", count(" + queryName + ") as count from " + entityname + " where " + queryName + " is not null group by " + group + " order by " + order + " " + sort);
    query.setFirstResult(0);  
    query.setMaxResults(10);  
    List names = query.list();
    Iterator iterator =  names.iterator();
    while(iterator.hasNext()) {
        System.out.println(iterator.next());
    }
    return query.list();
    }
    }); 
    }
    我加了显示hql查询结果的语句:List names = query.list();
    Iterator iterator =  names.iterator();
    while(iterator.hasNext()) {
        System.out.println(iterator.next());
    }打印结果如下(统计结果就应该是4条):[Ljava.lang.Object;@793542
    [Ljava.lang.Object;@1978b94
    [Ljava.lang.Object;@4cdac8
    [Ljava.lang.Object;@1afa486不知道这是什么意思
      

  3.   

    你以sql的形式把sql语句打印出来 对比一下
      

  4.   

    你查询出来的是object的数组,q.list()是List<Object[]> object[0] 是queryName ,object[1]是count
      

  5.   

    还有 你应该return  names 吧!
      

  6.   

    可能有些乱了.问题在于:
    1、统计功能不是返回所有字段,就两三个;
    2、统计功能会产生新的临时字段,比如要统计不同国家举办的会议次数。这个次数字段就是临时产生的;我现在程序出现问题的是:
    1、返回list,但是jsp页面里显示不出来;
    2、如果不返回list,该返回什么?数组?
    3、不知道是否JSP页面的标签使用出错?hql语句打印出来没问题
      

  7.   

    <s:iterator value="${availableItems}" status="tConference">
    这样行不!
      

  8.   

    你都这样子写了,还是别用HQL了.建议你这样用.创建一个SQL的查询结果集,addScalar方法设置要返回的字段,最后返回的集合其实里面放的全是Object数组,一个数组就是一条记录,里面分别放着你事先设置的字段的值,要按顺便喔源码如下public List<BbsWapThread> top5ByPid(String pid) { List<BbsWapThread> list = new ArrayList<BbsWapThread>();
    String sql = "select * from bbs_wap_thread where pid = ? and state = 1 order by time desc limit 1,5";
    Session session = getSession();
    try {
    List objectList = session.createSQLQuery(sql).addScalar("tid")
    .addScalar("title").setString(0, pid).list();
    for (Object o : objectList) {
    Object[] results = (Object[]) o;
    BbsWapThread thread = new BbsWapThread();
    Integer id = (Integer) results[0];
    String title = (String) results[1];
    if (title.length() > 18) {
    title = title.substring(0, 16) + "...";
    }
    thread.setId(id);
    thread.setTitle(title);
    list.add(thread);
    }
    } catch (HibernateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    super.releaseSession(session);
    } return list;
    }