Home IllegalStateException when trying to query a MongoDB domain class using Grails 2.3.7
Reply: 0

IllegalStateException when trying to query a MongoDB domain class using Grails 2.3.7

user2350 Published in March 23, 2018, 9:00 am

I am working on a legacy project that uses Grails 2.3.7 (with Maven) and Java 7, and I have to add a connection to a MongoDB database while keeping the existing Hibernate ones.

I have added the following to my pom.xml file:


And this to the BuildConfig.groovy file:

plugins {
    compile ':mongodb:3.0.2'
    compile 'org.grails.plugins:mongodb:3.0.2'    

(I have tried it both with and without the compile 'org.grails.plugins:mongodb:3.0.2' line)

On the DataSource.groovy file I have configured the db connection as follows:

grails {
    mongodb {
        host = "xxx.xxx.xxx.xxx"
        port = "27017"
        databaseName = "db"
        username = "user"
        password = "pass"

and the connection itself seems to be working, because if I change any value in there the Grails application does not even start.

I have then created a simple Domain class, Thingy.groovy:

class Thingy {

    String identifier  
    String description    

    static mapWith = "mongo"

    static constraints = {

And now, when I start the app, any call to methods of that class throws an IllegalStateException: "Method on class [Thingy] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.". However, if at the same place I call any methods of the old Domain classes that use the other datasource, they work like a charm.

Also, when starting the server I get another exception which I guess might be related, but I'm not sure what to do with it either: ERROR - Error configuring dynamic methods for plugin [mongodb:3.0.2]: org/grails/datastore/mapping/query/api/BuildableCriteria java.lang.NoClassDefFoundError: org/grails/datastore/mapping/query/api/BuildableCriteria.

I have also tried using the MongoDB plugin 3.0.3, but with the same results.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.355427 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO