我正在使用HQL通过DAO类获取数据,但它会抛出以下错误:
ERROR org.hibernate.hql.PARSER - <AST>:0:0: unexpected AST node: query
下面是我的Hql查询:
select new com.shaikh.dto.UserResult ( user.userSurName, avg((select avg(v1.age) from com.shaikh.dto.UserResult v1 where v1.joinDate between to_date(:dayFirst, 'dd-Mon-yy') and to_date(:dayLast, 'dd-Mon-yy') )), avg(user.age) ) from com.shaikh.dto.User user group by user.userSurName";
String [] paramNames = { "dayFirst", "dayLast" };
Object [] values = { firstDay,lastDay};
return getHibernateTemplate().findByNamedParam(queryString, paramNames, values);
我使用Oracle 11g作为数据库。
如果我用简单的avg(user.age)替换嵌套的avg()函数进行测试,它工作正常,所以看起来类映射工作正常。虽然我收到如上所述的错误,通知hql查询不正确。我不知道如何解决它。在此先感谢:)
谢谢&amp;问候,
Shariq
答案 0 :(得分:1)
您正在将HQL与SQL结合使用
to_date(:dayFirst, 'dd-Mon-yy') and to_date(:dayLast, 'dd-Mon-yy')
将这些参数作为日期参数传递。
答案 1 :(得分:0)
你不需要使用avg两次......这是正确的HQL:
select new com.shaikh.dto.UserResult ( user.userSurName, (select avg(v1.age) from com.shaikh.dto.UserResult v1 where v1.joinDate between :dayFirst and :dayLast), avg(user.age) ) from com.shaikh.dto.User user group by user.userSurName";
将dayFirst和dayLast传递给java.util.Date