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 8480783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:23:21+00:00 2026-06-10T19:23:21+00:00

Let’s say we have 3 domain classes: 2 classes related with each other through

  • 0

Let’s say we have 3 domain classes: 2 classes related with each other through a 3rd class.

Ok, some code:

class A {
     String subject
     String description

     static hasMany = [cs: C]

     static transients = ['numberOfCs']

     Long getNumberOfCs() {
         return cs.size()
     }
}

class B {
     String title
}

class C {
     A objectA
     B objectB

     static belongsTo = [a: A]
}

Pretty clear? I hope so. This work perfectly with my domain.

You can see the transient property numberOfCs, which is used to calculate the number of C instances related to my A object. And it works just fine.

The problem: listing all my A objects, I want to sort them by the number of relationships with C objects, but the transient property numberOfCs cannot be used for the scope.

How can I handle the situation? How can I tell GORM to sort the As list by numberOfCs as it would be a regular (non transient) field?

Thanks in advance.

  • 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-06-10T19:23:22+00:00Added an answer on June 10, 2026 at 7:23 pm

    I’m not sure that Grails’ criteria do support this, as you need both to select the A object itself and aggregate by a child objects (C). That means grouping by all the A‘s fields, which is not done automatically.

    If you only need some fields from A, you can group by them:

    def instances = A.withCriteria {
      projections { 
        groupProperty('subject')
        count('cs', 'cCount')
      }
      order 'cCount'
    }
    

    otherwise you’ll need to retrieve only ids and make a second query, like in this question.

    Another way is to use derived properties like described here (not sure it will work though):

    class A {
        static mapping = {
            numberOfCs formula: 'select count(*) from C where c.b_id = id'
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let's say I have the string: hello world; some random text; foo; How could
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let me explain best with an example. Say you have node class that can
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have this code: <p dataname=description> Hello this is a description. <a
Let's say you have a string that looks like this: token1 token2 tok3 And
Let's suppose I have this piece of code. foreach(string value in myList) { string
Let's say you have a class called Customer, which contains the following fields: UserName

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.