Greetings, I would like to order eagerly fetched domain objects that another domain class owns in a 1:m relationship, but cannot figure how to do this. When I try with a simplified project, I get an error. Below are my attempts:
class Picture {
String name
static hasMany = [comments:Comment]
static mapping = {
comments(lazy:false, sort:'content', order:'desc')
}
}
class Comment {
String content
Date dateCreated
static belongsTo = [Picture]
}
Now, when testing how the record fetch with println Picture.get(1) as JSON, I get the following error:
java.sql.SQLException: Column not found: COMMENTS0_.CONTENT in statement [select comments0_.picture_comments_id as picture1_0_, comments0_.comment_id as comment2_0_ from picture_comment comments0_ where comments0_.picture_comments_id=? order by comments0_.content desc
Without sort:'content', order:'desc', the Comments are in random order but no error.
I ended up doing the ordering after the list of children was already fetched, at the time I need them in the code. Something like this:
Has the advantage of allowing unlimited sorting configurations as needed, as opposed to only one as retrieved by default.