HQL 查询字段含有对象,怎么写

cailibin 2008-06-25 08:48:27
HQL 查询字段含有对象,怎么写

String queryString = " select model.opeUser, sum(model.mouthNum) from BetInfo as model "
+"where model.agentUser =? and model.opeTime >=? and opeTime<? and compleStore=?"
+" group by model.opeUser";
opeUser 是一个用户对象。
为什么执行总是报错。
谢谢大家
...全文
442 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
handong890 2008-06-27
  • 打赏
  • 举报
回复
要查对象 要配1对1关系
Landor2004 2008-06-25
  • 打赏
  • 举报
回复
也可以这样写,来返回OpeUser 对象,及其sum值
Iterator results = sess.createQuery(
"select model.opeUser, sum(model.mouthNum) from BetInfo as model "
+"where model.agentUser =? and model.opeTime >=? and opeTime <? and compleStore=? "
+"group by model.opeUser")
.list()
.iterator();

while ( results.hasNext() ) {
Object[] row = (Object[]) results.next();
OpeUser opeUser = (OpeUser) row[0];
Integer sum = (Integer) row[1];
.....
}
Landor2004 2008-06-25
  • 打赏
  • 举报
回复
你的意思是想返回user对象?那你的sum(xxx),在user对象里有位置吗
cailibin 2008-06-25
  • 打赏
  • 举报
回复
谢谢大家,
String queryString = " select model.opeUser.xxx, sum(model.mouthNum) from BetInfo as model "
+"where model.agentUser =? and model.opeTime >=? and opeTime <? and compleStore=?"
+" group by model.opeUser.xxx";

这样执行肯定是可以的。关键的问题是我想返回opeUser实例是属于User ,我想返回User 对象
Landor2004 2008-06-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 TRMeister 的回复:]
LS说的有道理。
LZ把错误搞出来看看。

直接查对象不知道成不成呀,有可能行。
但是group by 肯定有问题。

String  queryString = "  select model.opeUser, sum(model.mouthNum) from BetInfo  as  model    "
+"where  model.agentUser =? and model.opeTime >=? and opeTime <?  and compleStore=?"
+"  group  bymodel.opeUser.xxx";


[/Quote]

这样似乎不行,查询项目中不是分组的字段,必须要在group by中
String queryString = " select model.opeUser.xxx, sum(model.mouthNum) from BetInfo as model "
+"where model.agentUser =? and model.opeTime >=? and opeTime <? and compleStore=?"
+" group by model.opeUser.xxx";
JJTwo 2008-06-25
  • 打赏
  • 举报
回复
學習
zidasine 2008-06-25
  • 打赏
  • 举报
回复
用opeUser的属性试试
TRMeister 2008-06-25
  • 打赏
  • 举报
回复
LS说的有道理。
LZ把错误搞出来看看。

直接查对象不知道成不成呀,有可能行。
但是group by 肯定有问题。

String queryString = " select model.opeUser, sum(model.mouthNum) from BetInfo as model "
+"where model.agentUser =? and model.opeTime >=? and opeTime <? and compleStore=?"
+" group by model.opeUser.xxx";

Landor2004 2008-06-25
  • 打赏
  • 举报
回复
是一对一的关系吗,group by 不能根据对象吧,怎么的也有group by的字段吧
String queryString = " select o.xxx, sum(model.mouthNum) from BetInfo model left join fetch opeUser o "
+"where model.agentUser =? and model.opeTime >=? and opeTime <? and compleStore=?"
+" group by o.xxx";
这样试试
sallytonny 2008-06-25
  • 打赏
  • 举报
回复
学习。。。
wyhjia 2008-06-25
  • 打赏
  • 举报
回复
学习......

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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