开发者

Counting in hibernate using Criteria without unnecessary inner joins

I am using criteria for counting number of rows. consider a class

class GroupMembership{
 public Group  Group{get;set;}
 public Member Member{get;set;}

}

I have a criteria which fetches distinct members for each group as:

DetachedCriteria.For<GroupMembership>("this")
.CreateCriteria("Group", "grp")
.CreateAlias("this.CommunityMember", "CommunityMember")
.SetProjection(Projections.ProjectionList()
.Add(Projections.开发者_运维技巧GroupProperty("this.Group.Id"),"GroupId") 
.Add(Projections.CountDistinct("this.CommunityMember.Id"), "TotalMembers"))

Is there a way i can count without performing the inner join with the CommunityMember.

UPDATE: I am adding the SQLs generated from the query

SELECT   this_.GroupId                                  as y0_,
         count(distinct communitym2_.CommunityMemberId) as y1_
FROM     [GroupMembership] this_
         inner join [CommunityMember] communitym2_
           on this_.CommunityMemberId = communitym2_.CommunityMemberId
         inner join [Group] grp1_
           on this_.GroupId = grp1_.GroupId
GROUP BY this_.GroupId

As you can see we can easily avoid the inner join in the SQL. My question is whether the same can be done in criteria.


How about...

DetachedCriteria.For<GroupMembership>()
                .SetProjection(Projections.ProjectionList()
                    .Add(Projections.GroupProperty("Group"))
                    .Add(Projections.CountDistinct("CommunityMember")))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜