我正在开发一个使用 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,但结果相同。