Grails - sort by the domain relation attribute (using createCriteria())
Asked Answered
S

4

3

I have two domain classes with 1:n relationship:

import Action

class Task {    
    Action actionParent
    String taskName
}

and

class Action {
    String actionName
}

I have the list of Tasks where I have the column "Action name", I would like to sort this column by Action.actionName. Now I'm using the createCriteria() method [I need to use it because I have more logic for filtering and sorting...], but I'm able to sort only by "Action.id". This method looks like:

def criteria = Task.createCriteria();
taskList = criteria.list {
    if(parameters.max != null)
        maxResults(parameters.max)
    if(parameters.offset != null)
        firstResult(new Integer(parameters.offset))
    if(parameters.sort != null && parameters.order)
        order(parameters.sort, parameters.order)
}

Is there any way to sort the domain class data by the relationship attributes?

Thanks for any replay,

Mateo

Shatzer answered 1/8, 2010 at 18:34 Comment(1)
Mark as answered.Doubledealing
A
5

if you want to stick with the criteria approach, try something like this. Create an alias to you Action object and then access the property for sorting it

    def criteria = Task.createCriteria()
    def taskList = criteria.list {
        createAlias("actionParent","_action")
        order( "_action.actionName")
    }
Accumulation answered 1/8, 2010 at 19:50 Comment(2)
alternative without alias: def taskList = criteria.list { actionParent { order("actionName") } }Peachy
Although the answer and the comment look very promising and should work, they did not for me. It seems to be a bug in hibernate... The Answer from @jstricker helped me to sort by the property of an association by simply adding a param.sort = "actionParent.actionName".Eleen
S
4

I know this is an old question, but in my particular situation (wanting to sort on an association's association's field), it worked only when I passed the sort column directly into the list method:

def criteria = Task.createCriteria();
taskList = criteria.list(max: parameters.max, offset: parameters.offset, sort: parameters.sort, order: parameters.order) {
    // Other code here...
}

When I tried to put the ordering information in the closure itself, Hibernate barfed up an exception with a message org.hibernate.QueryException: could not resolve property: [Grails domain relation's relation's field] of: [Grails domain].

Szabadka answered 15/5, 2014 at 16:12 Comment(0)
P
2

Have you tried using the listOrderBy* GORM command, so it would be something like

def tasksList = Task.listOrderByActionName()
Phototransistor answered 1/8, 2010 at 19:45 Comment(0)
S
2

try this

 def tasksList = Task.findAll()
 tasksListSorted = tasksList.sort { it.actionParent.actionName }

you can also do this

 tasksList.sort{
        task1, task2 -> task1.actionParent.actionName.compareToIgnoreCase(task2.actionParent.actionName)

    }
Stjohn answered 13/10, 2015 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.