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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:15:36+00:00 2026-05-14T23:15:36+00:00

It seems that there are two different ways of declaring sorted associations in Grails

  • 0

It seems that there are two different ways of declaring sorted associations in Grails :

Method 1 (see here) using default sort order

class Book {
  String title 
}
class Author {
  static hasMany = [books : Book]
  static mapping = { books sort: "title"}
}

Method 2 (see here) using SortedSet

class Book implements Comparable {
  String title
  int compareTo(obj) {
    title <=> obj.title
  }
}
class Author {
  SortedSet books
  static hasMany = [books : Book]
}

I am not sure which one to use and what is the difference (if any), pros and cons between using one against the other.

I would appreciate any clarification.

Thank you

  • 1 1 Answer
  • 1 View
  • 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-14T23:15:36+00:00Added an answer on May 14, 2026 at 11:15 pm

    I started to dig into how this all works and then found that method 1 is actually busted in current versions of grails (tested in both 1.2.1 and 1.3). When you actually try to retrieve an author and look at it’s books, it throws an exception

    There is an open defect for it (4089) which has been open for quite a while.

    Here’s the exception that gets thrown:

    ERROR util.JDBCExceptionReporter  - Column not found: BOOKS0_.TITLE in statement [select books0_.author_books_id as author1_0_, books0_.book_id as book2_0_ from author_book books0_ where books0_.author_books_id=? order by books0_.title]
    

    If and when they finally fix it, the differences between the two methods are that in method one, sorting is done at the database level. As you can see in the exception above, GORM was trying to do an “order by books0_.title” which would use any database index on the book.title field and return the objects in that order.

    The second method would sort the objects in memory at the time that they get inserted into the set (using the compareTo method that was defined).

    Until the current bug is fixed, I’d use method 2 because it’s the only thing that works. It should be fine for relatively small collections of things. After it’s fixed, I’d potentially prefer method 1 as the database should be quicker at doing the sorting with an index on the sort field.

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

Sidebar

Related Questions

In Visual Basic 2008, there's two different ways that I know of to accomplish
It seems to me that there are two scenarios in which to use JOINs:
It seems that these two operators are pretty much the same - is there
I've come across two different ways to define/name objects and functions in JavaScript that
When using the Entity Framework there are basically two ways to create your model.
I have an application that the user can start in two different ways, as
When doing web programming there seem to be two different ways of handling a
When defining fields in a django model, there are two ways to say that
There are two things that seem to be popular nowadays and I was wondering
It seems that there is a implementation of rope in my /usr/include/c++/4.5.1/ext/rope (and ropeimpl.h

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.