Case-insensitive order on nested properties

734 Views Asked by At

Say, I have two domain classes:

class Foo {
   Bar bar

   Long baz
}

class Bar {
   String name
}

And I have a call coming into a createCriteria that wants to sort by the Bar's name (or any other valid property that the client wants to sort by), ignoring the case. I've been able to reduce it to:

Foo.createCriteria().list() {
    //...
    bar {
        order((params.sortOrder == 'asc' ? 
            Order.asc('name') : // 'name' is a variable in the real code 
            Order.desc('name')
        ).ignoreCase())
    }
}

but when the sort property is nested (ie a property of bar), I get an exception:

org.hibernate.QueryException: could not resolve property: name of: Foo

I've found these related Grails bugs GRAILS-8182, GRAILS-9171, GRAILS-3911 which make it seem like there is a combination of bugs that make what I want to do, not possible.

Is there a way to do case-insensitive sorting on nested properties in createCriteria? I realize I can sort either on the client, or on the Groovy list, but I really would prefer not to have to reinvent the wheel (especially since it would be a really ugly wheel).

I am using Grails 2.2.4.

1

There are 1 best solutions below

0
On

I stumbled upon this question when trying to solve a similar issue related to sorting in grails. Maybe in the meanwhile a solution has already been found. If not, I wanted to suggest you tried the following:

Foo.createCriteria().list() {
    createAlias("bar","_b")
    order("_b.name", (params.sortOrder == "asc")?"asc":"desc")
}

In this way you should be able to order instances of Foo by bar.name