Hibernate group by criteria将不需要的项添加到选择列表中

时间:2014-04-28 18:47:15

标签: java hibernate hibernate-criteria

有没有办法编写Hibernate标准,以便它不包含使用选择列表中的Projection.groupProperty添加的属性?

我按照以下方式创建我的投影列表:

ProjectionList projectionList = Projections.projectionList();

// I want these values included in the select list
projectionList
 .add(Projections.distinct("CAMPAIGN")) 
 .add(Projections.rowCount());

// I DO NOT want to select these values.  I only need the "group by"
projectionList.add(Projections.groupProperty("CAMPAIGN"))
 .add(Projections.groupProperty("ad.ID"))
 .add(Projections.groupProperty("region.ID"));

现在,生成以下查询:

SELECT DISTINCT this_.CAMPAIGN AS y0_, 
                count(*)       AS y1_, 
                this_.CAMPAIGN AS y2_, 
                ad2_.ID        AS y3_, 
                region3_.ID    AS y4_ 
FROM   account this_ 
       INNER JOIN campaign campaign1_ 
               ON this_.CAMPAIGN = campaign1_.ID 
       INNER JOIN ad ad2_ 
               ON campaign1_.ID = ad2_.CAMPAIGN 
       INNER JOIN region region3_ 
               ON ad2_.ID = region3_.AD 
WHERE  this_.VENDOR = 4 
GROUP  BY this_.CAMPAIGN, 
          ad2_.ID, 
          region3_.ID 

如果最后三个项目不在选择列表中,则会返回许多额外的行。

我想创建相同的查询WITHOUT

this_.CAMPAIGN as y2_, ad2_.ID as y3_, region3_.ID as y4_

作为选择列表的一部分。

也就是说,有没有办法GROUP BY项目,而不是将它们包含在选择列表中?

0 个答案:

没有答案