日期:2014-05-19  浏览次数:20686 次

关于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>