首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

关于Hibernate的HQL话语实现查询统计功能

2012-12-27 
关于Hibernate的HQL语句实现查询统计功能在查询分析器中,可以得出正确的结果。select a.name,sum(t.actualU

关于Hibernate的HQL语句实现查询统计功能
在查询分析器中,可以得出正确的结果。


select a.name,sum(t.actualUseNumber) 测样数目
from testTotal t,adminInfo a,order_instrument o
where a.id = t.adminId and o.id = t.instrumentId and sysTime between '2012-10-01' and '2012-12-01'
group by a.name


由于项目使用的是HQL语句,
转换的时候:

public List<TestTotal> findBySysTime(Timestamp startTime,Timestamp endTime) {
return this.getHibernateTemplate().find("select t.adminInfo.name, count(t.actualUseNumber) from TestTotal as t where t.sysTime bewteen '"+startTime+"' and '"+endTime+"'"+" group by t.adminInfo.name");
}


一直报错:

org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: bewteen near line 1, column 105 [select t.adminInfo.name, count(t.actualUseNumber) from com.yaxing.entity.TestTotal as t where t.sysTime bewteen '2012-11-01 15:14:11.0' and '2012-11-14 15:14:14.0' group by t.adminInfo.name]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: bewteen near line 1, column 105 [select t.adminInfo.name, count(t.actualUseNumber) from com.yaxing.entity.TestTotal as t where t.sysTime bewteen '2012-11-01 15:14:11.0' and '2012-11-14 15:14:14.0' group by t.adminInfo.name]
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:660)
org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)



其实要实现的功能就是按人员来统计个数(actualNumber)


[最优解释]


<s:iterator value="listTestTotal" id="listTestTotal" status="a"> 
 

<s:if test="listTestTotal!=null"> 
<tr>
<td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1"> <s:property value="#a.index+1"/></span> </div></td>
<td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1"><s:property value="#listTestTotal[0]"/></span></div></td>
                                            <td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1"><s:property value="#listTestTotal[1]"/> </span></div></td>
                                           <td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1"><s:property value="#listTestTotal[2]"/> </span></div></td>

                                           


                                     </tr>
                                    </s:if>
                                  
                                    
</s:iterator>  



页面这样取值就可以了。
[其他解释]
先不管它能不能执行,between你都敲错了。
[其他解释]
当Object数组取。
[其他解释]
hql 是面向对象的,照你这个sql应该是原生的sql,不应该用hql查询,应该createQuery
[其他解释]
hibernate不适合做统计。而且统计的返回类型不是对象的。非要用hibernate的话用Projections.count函数
[其他解释]
直接使用 session.createSQLQuery 也可以的。
[其他解释]
引用:
先不管它能不能执行,between你都敲错了。
额,确实是写错了。

改了之后,后台输出sql语句如下:

Hibernate: 
    select
        admininfo1_.name as col_0_0_,
        count(testtotal0_.actualUseNumber) as col_1_0_ 
    from
        orderSys.dbo.testTotal testtotal0_,
        orderSys.dbo.adminInfo admininfo1_ 
    where
        testtotal0_.adminId=admininfo1_.id 
        and (
            testtotal0_.sysTime between '2012-11-01 16:49:58.0' and '2012-11-14 16:50:01.0'
        ) 
    group by
        admininfo1_.name


但是页面取值的时候出错了,有个疑问,我这样查出来的数据怎么显示在页面上呢?
List<TestTotal> list集合里面存放的是对象,但是我直接取属性的时候页面报错,
是不是页面应该取那个统计后的数据呢?该怎么写呢,谢谢。
[其他解释]
引用:
先不管它能不能执行,between你都敲错了。


调试的时候,值出来了,就是页面该写啥字段呢?

[其他解释]
引用:
直接使用 session.createSQLQuery 也可以的。

谢谢,结果已经出来了,就是页面取值的时候,不知道咋写,因为这不是对象.属性能出来的啊

是统计的值


[其他解释]
如果返回的不是对象,则当做Object数组处理...

热点排行