請教Hibernate hql中sum()的用法!

xhwly 2005-11-22 02:22:41

這個hql查詢語句該如何寫?

public Double getSumMoney(So so) {
try {
List l = this.getHibernateTemplate().find( "select sum(soit.stkQty*soit.salePric) from SoIt soit where soit.so=?";, so);
if (l != null && !l.isEmpty()) {
double s = ( (Double) l.get(0)).doubleValue();
return new Double(s);
}
else {
return new Double(0);
}
}
catch (DataAccessException ex) {
logger.error(ex);
return new Double(0);
}
}

find 方法中如果查詢語句用 "select sum(soit.salePric) from SoIt soit where soit.so=?"
則不會報錯,但是象getSumMoney方法中的寫法就報如下錯
Hibernate: select count(so0_.ID) as x0_0_ from TOG_So so0_ where (1=1 )
Hibernate: select top 1 so0_.ID as ID, so0_.SoNo as SoNo, so0_.ShopId as ShopId, so0_.CustId as CustId, so0_.CrtMan as CrtMan, so0_.PostMan as PostMan, so0_.Status as Status from TOG_So so0_ where (1=1 ) order by so0_.ID asc
Hibernate: select soit0_.SoId as SoId__, soit0_.ID as ID__, soit0_.ID as ID0_, soit0_.ItNo as ItNo0_, soit0_.ItDesc as ItDesc0_, soit0_.StkQty as StkQty0_, soit0_.SalePric as SalePric0_, soit0_.SoId as SoId0_ from TOG_SoIt soit0_ where soit0_.SoId=? order by soit0_.Id asc
Hibernate: select sum(soit0_.StkQty) as x0_0_ from TOG_SoIt soit0_ where (soit0_.SoId=? )
Hibernate: select sum(soit0_.StkQty*soit0_.SalePric) as x0_0_ from TOG_SoIt soit0_ where (soit0_.SoId=? )
2005-11-21 08:49:14,155 [net.sf.hibernate.util.JDBCExceptionReporter]-[WARN] SQL Error: 0, SQLState: 42S22
2005-11-21 08:49:14,171 [net.sf.hibernate.util.JDBCExceptionReporter]-[ERROR] [Microsoft][SQLServer 2000 Driver for JDBC]Invalid column name: x1_0_

提示為無效的列名x1_0_,但從輸出的sql語句中根本就找不到x1_0_,並且這些語句在sql server的查詢分析器中都能正確執行.

...全文
746 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xhwly 2005-11-25
  • 打赏
  • 举报
回复
我在持久類中增加一個相當於計算列的屬性,還是報錯.

如果在.hbm.xml中增加計算列,不知道該如何映射.

有知道的朋友幫幫忙啊!
xhwly 2005-11-23
  • 打赏
  • 举报
回复
請問應該是在.hbm.xml文件中增加computed Column呢?還是在持久類中增加一個屬性?
xhwly 2005-11-23
  • 打赏
  • 举报
回复
to yinleiyoung(星际孤虹) :
謝謝,我試試看.
yinleiyoung 2005-11-23
  • 打赏
  • 举报
回复
hibernate2里面是不支持这种写法的
This seems to common behaviour in hiberbate 2, which should vanish with the new compiler of version 3
However, in the older version mathematical expressions within the SELECT clause are not supported, but only in the WHERE clause.
You could add a computed column to the mapping or compute the values in Java rightaway
xhwly 2005-11-22
  • 打赏
  • 举报
回复
自已頂一下.

81,092

社区成员

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

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