简体   繁体   English

Grails - 按域关系属性排序(使用createCriteria())

[英]Grails - sort by the domain relation attribute (using createCriteria())

I have two domain classes with 1:n relationship: 我有两个域关系:1:n关系:

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. 我有任务列表,其中我有“操作名称”列,我想通过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". 现在我正在使用createCriteria()方法[我需要使用它,因为我有更多的逻辑用于过滤和排序...],但我只能按“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 圣马特奥

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 为您创建Action对象的别名,然后访问该属性以对其进行排序

    def criteria = Task.createCriteria()
    def taskList = criteria.list {
        createAlias("actionParent","_action")
        order( "_action.actionName")
    }

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: 我知道这是一个老问题,但在我的特殊情况下(想要对关联的关联字段进行排序),它只有在我将排序列直接传递给list方法时才有效:

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] . 当我试图将排序信息放入闭包本身时,Hibernate用消息org.hibernate.QueryException: could not resolve property: [Grails domain relation's relation's field] of: [Grails domain]异常org.hibernate.QueryException: could not resolve property: [Grails domain relation's relation's field] of: [Grails domain]

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)

    }

您是否尝试过使用listOrderBy * GORM命令,因此它会是这样的

def tasksList = Task.listOrderByActionName()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM