1
很抱歉,如果这是一个愚蠢的问题,但我坚持这个问题了整整一个下午,但无法找到一个解决方案,因为我不熟悉复杂的SQL:Hibernate选择groupProperty,rowCount与rowCount> n?
我想找到“前n个MESSAGE-用味精发送计数>阈发送用户从表“这是我的准则:
Criteria c = session.createCriteria(Message.class);
ProjectionList plist = Projections.projectionList();
plist.add(Projections.groupProperty("user"));
plist.add(Projections.rowCount() , "count");
c.setProjection(plist);
c.addOrder(Order.desc("count"));
c.setFirstResult(0);
c.setMaxResults(count);
这是我可以写,但它缺乏的” 过滤行与rowCount时超过某个阈值低”。如何使用标准实现它?非常感谢 !
--------------更新------------------------
谢谢@TheStijn , 我试过了 。我现在可以使用子查询来实现我的目标,但生成的查询是不是很聪明!查看生成的SQL:
select
this_.fromUser as y0_,
count(*) as y1_
from
Message this_
where
this_.fromUser is not null
and this_.created>?
and this_.created<?
and ? <= (
select
count(*) as y0_
from
Message msg_
where
msg_.fromUser=this_.fromUser
and msg_.fromUser is not null
and msg_.created>?
and msg_.created<?
)
group by
this_.fromUser
order by
y1_ desc limit ?
也就是说,子查询重复最主查询的,我认为这是一个有点多余。是否有任何建立这样的SQL查询的标准:
select
this_.fromUser as y0_,
count(*) as y1_
from
Message this_
where
this_.fromUser is not null
and this_.created>?
and this_.created<?
and y1_ > ? // threshold
group by
this_.fromUser
order by
y1_ desc limit ?
非常感谢!
(这似乎更容易使用HQL要做到这一点,但我对标准的方式好奇)
嗨,我试过了,但得到了'不那么聪明'的SQL查询。你能再看一遍吗?谢谢 。 – smallufo 2011-03-24 15:01:18
你不能在SQL中做一些不可能的事情。由于你不能在sql中的where子句中使用聚合变量(不是在我知道的任何数据库中),所以你不能在休眠中使用)。如果你的上面的sql正在工作,请告诉我你正在使用哪个数据库。 – 2011-03-24 15:32:25
谢谢,你是对的,它不起作用。 – smallufo 2011-03-25 03:38:45