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

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

LuTHieR
1#
LuTHieR Published in 2017-12-05 08:52:29Z

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:

<dependency>
    <groupId>org.grails.plugins</groupId>
    <artifactId>mongodb</artifactId>
    <type>zip</type>
    <version>3.0.2</version>
</dependency>

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.

LuTHieR
2#
LuTHieR Reply to 2017-12-05 10:21:35Z

This answer https://stackoverflow.com/a/35710495/451420 gave me a clue. I had to update the grails-datastore-core and grails-datastore-gorm versions manually as well:

<dependency>
    <groupId>org.grails</groupId>
    <artifactId>grails-datastore-gorm</artifactId>
    <version>3.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.grails</groupId>
    <artifactId>grails-datastore-core</artifactId>
    <version>3.1.4.RELEASE</version>
</dependency>

In case it helps anyone else, I found out which versions to use by looking at the <dependencies> inside the POM file of the mongodb plugin (https://repo.grails.org/grails/plugins/org/grails/plugins/mongodb/3.0.3/mongodb-3.0.3.pom)

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO