Grails - 按域关系属性排序(使用 createCriteria())
我有两个具有 1:n 关系的域类:
import Action
class Task {
Action actionParent
String taskName
}
并且
class Action {
String actionName
}
我有任务列表,其中有“操作名称”列,我想按 Action.actionName 对此列进行排序。现在我正在使用 createCriteria() 方法 [我需要使用它,因为我有更多的过滤和排序逻辑...],但我只能按“Action.id”进行排序。这个方法看起来像:
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)
}
有没有办法按关系属性对领域类数据进行排序?
感谢您的重播,
马特奥
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想坚持使用标准方法,请尝试这样的方法。创建 Action 对象的别名,然后访问属性以对其进行排序
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
我知道这是一个老问题,但在我的特殊情况下(想要对关联的关联字段进行排序),只有当我将排序列直接传递到列表方法中时它才起作用:
当我尝试将排序信息放入闭包中时本身,Hibernate 抛出了一个异常,并显示消息 org.hibernate.QueryException: 无法解析属性:[Grails 域关系的关系的字段] of: [Grails 域]。
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:
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]
.您是否尝试过使用 listOrderBy* GORM 命令,所以它会类似于
Have you tried using the listOrderBy* GORM command, so it would be something like
试试这个
你也可以这样做
try this
you can also do this