DetachedCriteria list methor with order param in nested property

55 Views Asked by At

I want to use the DetachedCriteria's list method with a domain with nested properties and order by one of the attributes in one of those properties.

I'm trying creating an alias criteria.createAlias("user", "user") and then listing: def userRoleList = criteria.list(max: params.length, offset: params.start,sort: sort, order: orderDir)

I have these classes:

class UserRole implements Serializable {
    ...
    User user
    Role role
    ...
}



class User implements Serializable {
    ..
    String username
    String password
    boolean enabled
    ...
}

And I want to order by username or if it's enabled or not, but it keeps saying

   org.hibernate.QueryException: could not resolve property: user.username of: ...model.security.UserRole

all the time, is there any way to do this properly?

1

There are 1 best solutions below

3
Jeff Scott Brown On

You should be able to specify sort order like this:

UserRole.where {
    // put your query criteria here
    // ...
}.list(sort: 'user.username', offset: 0, order: 'asc')