Search code examples
mongodbgrailsgrails-ormgrails-2.4

grails mongodb 3.0.2 plugin join queries on associations using createCriteria()


I am trying to search for users with particular role using createCriteria().I have three domain classes: User, Role and UserRole (they are from SpringSecurity).

class UserRole implements Serializable {

    User user
    Role role

    static constraints = {
        user nullbale: false
        role nullable: false
    }

    static mapping = {
        version: false
        id composite: ['role', 'user']
    }
}

I am searching on isActive, name or username fields on User Domain and authority field on Role Domain .

    def c = UserRole.createCriteria()
    def results = c {
        user {
            eq("isActive", true)
            or {
                ilike("name", "somename")
                ilike("username", "someusername")
            }
        }
        role {
            eq("authority", "ROLE_USER")
        }
    }

On running this I get UnsupportedOperationException from mongo plugin. Here is stacktrace

ERROR errors.GrailsExceptionResolver  - UnsupportedOperationException occurred when processing request: [GET] /users/search - parameters:
query: 
Join queries are not supported by MongoDB. Stacktrace follows:
Message: Join queries are not supported by MongoDB
    Line | Method
->>  162 | handle             in org.grails.datastore.mapping.mongo.query.MongoQuery$2
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    142 | handle             in     ''
|   1091 | populateMongoQuery in org.grails.datastore.mapping.mongo.query.MongoQuery
|    993 | executeQuery       in org.grails.datastore.mapping.mongo.query.MongoQuery$58
|    861 | doInDB . . . . . . in     ''
|    833 | doInDB             in     ''
|    542 | list . . . . . . . in org.grails.datastore.mapping.query.Query
|    325 | invokeMethod       in grails.gorm.CriteriaBuilder
|     17 | $tt__searchUsers . in com.themopi.apis.searchactivity.SearchService
|     29 | searchUser         in com.themopi.apis.search.SearchController
|    198 | doFilter . . . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter           in grails.plugin.cache.web.filter.AbstractFilter
|    106 | processFilterChain in com.odobo.grails.plugin.springsecurity.rest.RestTokenValidationFilter
|     72 | doFilter           in     ''
|     53 | doFilter . . . . . in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
|   1145 | runWorker          in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run                in java.lang.Thread

I found a Jira on this issue but it is unresolved.

Any work around for this till now as jira was created on 09/Mar/12 or I am missing something?

I will be using pagination and projections in this query?

Any help or guidance is highly appreciated.

Thanks in advance.


Solution

  • there's an excellent article from Burt on this topic.

    One of the main parts of it is, that the security model should be implemented with embedded/sub-doc entities. Actually joins shouldn't be used with mongo at all, and if you need those, use a RDBMS or Graph DB.