简体   繁体   中英

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

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.

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM