我正在使用Grails 2.3.7(与Maven)和Java 7一起使用的旧项目,在保留现有Hibernate数据库的同时,我必须添加与MongoDB数据库的连接。
我已经将以下内容添加到了 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'
}
(无论是否使用编译'org.grails.plugins:mongodb:3.0.2'行,我都尝试过)
在 DataSource.groovy 文件上,我将数据库连接配置如下:
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,但结果相同。
最佳答案
这个答案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>
万一它对其他人有帮助,我通过查看mongodb插件(https://repo.grails.org/grails/plugins/org/grails/plugins/mongodb/3.0.3/mongodb-3.0.3.pom)的POM文件中的
<dependencies>
找出了要使用的版本。
关于mongodb - 尝试使用Grails 2.3.7查询MongoDB域类时出现IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47649496/