根据grails中的父属性对查询结果进行排序 [英] Sorting query results according to parent property in grails
本文介绍了根据grails中的父属性对查询结果进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
grails中是否有可能根据关系中父类的属性对查询结果进行排序。例如。我有一个与任务列表有一对多关系的用户域类。
Is it possible in grails to sort query results according to a property of a parent class in a relationship. E.g. I have a user domain class which has a one to many relationship with a list of Tasks.
当显示Tasks域类的列表页面时,我希望能够
When showing the list page of the Tasks domain class, I want to be able to sort the list of tasks by the associated User name.
class User {
String name
String login
String group
List tasks = new ArrayList()
static hasMany = [tasks:Task]
}
class Task {
String summary
String details
User user
static belongsTo = [user:User]
}
我可以这样做
I can do something like this
Task.list([sort:"user", order:"asc"])
但是这样的user.id,有没有办法指定排序在user.name?
But this sorts by the user.id, is there a way to specify the sort to be on the user.name?
推荐答案
您可以使用 Criteria
def criteria = Task.createCriteria()
def taskList = criteria.list {
createAlias("user","_user")
order( "_user.name")
}
这篇关于根据grails中的父属性对查询结果进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文