0

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 Answers1

0

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')
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • this is not working, actually the sort variable contains "user.whatever" and still gives this error – Alejandro Pacheco Tejeda Sep 09 '21 at 08:34
  • "this is not working, actually the sort variable contains "user.whatever" and still gives this error" - I cannot reproduce that. See https://github.com/jeffbrown/alejandropachecotejedauserrole/blob/da0fd295edc8e215b63bf3468f9b96b3d14db42f/grails-app/controllers/userrole/DemoController.groovy. If you run the app and send a request to `/demo` you should see the following SQL logged to stdout: https://gist.github.com/jeffbrown/3cf10eebc45599a9e1f42c2fe7eb6809 – Jeff Scott Brown Sep 09 '21 at 13:27
  • Can you tell me which Hibernate dialect and which JDBC driver you are using? – Jeff Scott Brown Sep 09 '21 at 13:30