Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4028280
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:12:04+00:00 2026-05-20T11:12:04+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:12:04+00:00Added an answer on May 20, 2026 at 11:12 am

    How about…

    DetachedCriteria.For<GroupMembership>()
                    .SetProjection(Projections.ProjectionList()
                        .Add(Projections.GroupProperty("Group"))
                        .Add(Projections.CountDistinct("CommunityMember")))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set of records which I need to search using criteria. But criteria
I'm using jpa 2.0 and I have the following entity: @Entity public class Folder{
how can we get distinct result by using criteria in hibernate.
public MyClass(int someUniqueID) { using(//Session logic) { var databaseVersionOfMyClass = session.CreateCriteria(/*criteria*/) .UniqueResult<MyClass>(); //Load logic
I still have problem with fetching first location on android. I am using Criteria
I have created a Hibernate (3.5) Criteria query: Criteria criteria = db.getSession().createCriteria(Vendor.class); criteria.add(Restrictions.isEmpty(models)); When
I have a question using Hibernate Criteria, I need to convert this query using
Is there any way to get a PagedResultList in grails without using criteria? I
I have following class ( entity ) @Entity public class Magazine { private int
I have retrieve the Order table form database using hibernate criteria option. In my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.