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

  • SEARCH
  • Home
  • 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 335835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:11:09+00:00 2026-05-12T10:11:09+00:00

Assuming I have classes User and UserGroup. There is an optional 1-many group to

  • 0

Assuming I have classes User and UserGroup. There is an optional 1-many group to user association and the association is mapped from both sides (the UserGroup side via a property called “members”, which is a HashSet, the User side via property “group”).

Using the Criteria API, how can I query for all groups, sorted by count of group members?

(Edit) I should have pointed out when I asked this that pagination was being performed in the SQL, so the ordering should also be performed in the SQL if possible.

  • 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-12T10:11:10+00:00Added an answer on May 12, 2026 at 10:11 am

    Another option would be to use the @Formula annotation which is basically the equivalent of creating a nested select so you can get at your counts.

    On your DB objects property / member in question (which you should create), add the annotation to the getter like:

    @Formula(“(SELECT COUNT(TABLE_NAME.ID) FROM TABLE WHERE whatever…)”)
    public long getNewProperty{
    return newProperty;
    }

    Then target the member directly to get hold of your count values…

    Note: Be aware that when specifying the sql within the formula annotation, you must specify the field names directly when referencing the current objects table (this) as hibernate deals with this itself. So in your WHERE clause just use ID or whatever on its own when referencing the current objects table which I suspect is UserGroup.

    I had to have a setter for my new property in order for hibernate to work and prevent the compiler from complaining.

    There may be other clever ways of using @Formula, but I’m quite new to this and there doesn’t appear to be very much documentation on the subject…

    If you’ve never used nested SELECTS before, it might be an idea for you to just recreate in sql first – that helped me.

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have a system of three Classes. The GameClass creates instances of both
Assuming I have fonts installed which have the appropriate glyphs in them, is there
Assuming i have the following classes: Class MainClass { private OtherClass1; MainClass() { OtherClass1
Assuming I have an array of User objects where each object is actually one
I have two classes Foo and Bar mapped to two different tables, and I'd
I have a collection of classes defined from UIView. @interface { UIView *thisView UIView
Assuming I have the following classes in different files: <?php namespace MyNS; class superclass
Assuming I have the following classes in an application: A base class: public abstract
Assuming I have only the class name of a generic as a string in
Assuming I have an open source web server or proxy I can enhance, let's

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.