Grails: sort by nested attributes

Is it possible to sort by nested attributes using queries?

I have 2 domain classes:

class Parent { String name Child child } 

and

 class Child { String name static belongsTo = [parent: Parent] } 

It works:

 Parent.where {}.list(sort: 'name') 

and this is not so:

 Parent.where {}.list(sort: 'child.name') 

I have an error:

 could not resolve property: child.name of: Parent 

I am using grails 2.3.x

+6
source share
1 answer

See: Grails - sort by domain relationship attribute (using createCriteria ())

Solution 1:

  def criteria = Child.createCriteria(); println criteria.list{ createAlias("parent","_parent") order( "_parent.name") } 

Solution 2:

  def criteria = Child.createCriteria(); println criteria.list{ parent { order("name") } } 

Solution 3:

 class Child { String name static belongsTo = [parent: Parent] public String getParentName(){ return parent.getName() } } println Child.listOrderByParentName() 

Hope this helps.

+9
source

Source: https://habr.com/ru/post/970527/


All Articles