hibernate执行hql语句问题!!

corruptsatan 2010-06-17 08:41:10
String hql = "select new map(convert(char(10),a.testDate,111) as testDate,sum(totalCase) as totalCase,sum(okCase) as okCase,sum(pokCase) as pokCase,sum(ntCase) as ntCase,sum(ngCase) as ngCase) " +
"from AutoTestRecord as a " +
"where a.testDate between '"+DateUtil.formatDate(beginDate)+"' and '"+DateUtil.formatDate(endDate)+"' group by convert(char(10),a.testDate,111)";

执行时报错。
java.lang.NullPointerException
org.hibernate.hql.NameGenerator.generateColumnNames(NameGenerator.java:23)
org.hibernate.hql.ast.util.SessionFactoryHelper.generateColumnNames(SessionFactoryHelper.java:386)
org.hibernate.hql.ast.tree.SelectClause.initializeColumnNames(SelectClause.java:247)
org.hibernate.hql.ast.tree.SelectClause.finishInitialization(SelectClause.java:237)
org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:231)
org.hibernate.hql.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:704)
。。。
。。。

改成这样就可以:
String hql = "select new map('2010-06-17' as testDate,sum(totalCase) as totalCase,sum(okCase) as okCase,sum(pokCase) as pokCase,sum(ntCase) as ntCase,sum(ngCase) as ngCase) " +
"from AutoTestRecord as a " +
"where a.testDate between '"+DateUtil.formatDate(beginDate)+"' and '"+DateUtil.formatDate(endDate)+"' group by convert(char(10),a.testDate,111)";

因为testDate为datetime类型,而我只要group by 日期,所以必须转换,但convert函数在group by后面是可以,但前面我也要select出来用,但就是报错。
请高手指点!!
...全文
492 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
回楼主:"这贴在留几天,希望高手可以讲解下convert函数出错的原因。"

你留几天,等高手来解决,你自己都说不定已经解决了。等不了高手你就会自己解决的了。希望你快速解决
corruptsatan 2010-06-18
  • 打赏
  • 举报
回复
convert函数放到select后面就会报错,不知道什么原因。

我最后是用max函数解决问题的,反正我只关心日期,后面的时间取哪个都一样,所以max和min都可以。

String hql = "select new map(max(testDate) as testDate,sum(totalCase) as totalCase,sum(okCase) as okCase,sum(pokCase) as pokCase,sum(ntCase) as ntCase,sum(ngCase) as ngCase) " +"from AutoTestRecord " +"where testDate between '"+DateUtil.formatDate(beginDate)+"' and '"+DateUtil.formatDate(endDate)+"' group by convert(char(10),testDate,111)";

遇到同样问题的兄弟,可以参考我的办法。

这贴在留几天,希望高手可以讲解下convert函数出错的原因。
i李小武 2010-06-18
  • 打赏
  • 举报
回复
可能是日期格式不匹配吧
qiheia 2010-06-18
  • 打赏
  • 举报
回复
你的值有空,检查下你的参数值吧。。。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧