0

我正在开发一个使用 Grails 2.3.7(使用 Maven)和 Java 7 的遗留项目,我必须添加到 MongoDB 数据库的连接,同时保留现有的 Hibernate 数据库。

我已将以下内容添加到我的pom.xml文件中:

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

这对BuildConfig.groovy文件:

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

(我已经尝试过使用和不使用compile 'org.grails.plugins:mongodb:3.0.2'行)

DataSource.groovy文件中,我配置了 db 连接,如下所示:

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

并且连接本身似乎正在工作,因为如果我更改其中的任何值,Grails 应用程序甚至都不会启动。

然后我创建了一个简单的 Domain 类Thingy.groovy

class Thingy {

    String identifier  
    String description    

    static mapWith = "mongo"

    static constraints = {
    }
}

现在,当我启动应用程序时,对该类的任何方法的调用都会引发 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."。但是,如果我在同一个地方调用使用其他数据源的旧 Domain 类的任何方法,它们的工作就像一个魅力。

此外,在启动服务器时,我得到另一个异常,我猜这可能是相关的,但我也不确定如何处理它: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.

我也尝试过使用 MongoDB 插件 3.0.3,但结果相同。

4

1 回答 1

1

这个答案https://stackoverflow.com/a/35710495/451420给了我一个线索。我还必须手动更新grails-datastore-core和版本:grails-datastore-gorm

<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>

如果它对其他人有帮助,我通过查看<dependencies>mongodb 插件的 POM 文件(https://repo.grails.org/grails/plugins/org/grails/plugins/mongodb/3.0 .3/mongodb-3.0.3.pom )

于 2017-12-05T10:21:35.473 回答