2
votes

I have a domain class that has a 'hasMany' relationship that I would like to sort on so results are consistent when retrieving. Below is an example of the domain class.

class Author {

    static hasMany = [ books: Book ]

    static mapping = {
        books sort: 'title', order: 'asc'
    }
}

This produces the following error.

Default sort for associations [Author->books] are not supported with unidirectional one to many relationships.

How can I sort on title in this example?

I have been able to achieve sorting on another hasMany relationship. Any feedback would be most appreciated.

2

2 Answers

1
votes

As the error suggests you need to make the relationship bidirectional for default order to work,

just add the following in Book domain

static belongsTo = [author:Author] if you need default sort order to work.

0
votes

Check if it will work for you

class Author {
    SortedSet books  // add this to your Author domain

    static hasMany = [books: Book]
} 

class Book implements Comparable {
    String title

    int compareTo(obj) {
        title.compareTo(obj.title)
    }
}