将域类从GORM独立模块导入Grails [英] Importing domain classes from GORM-standalone module into Grails
问题描述
我有2个难题:
1)名为core-module
的无Grails项目,具有独立的GORM:
1) a no-Grails project named core-module
with standalone GORM:
dependencies {
compile 'org.grails:grails-datastore-gorm-mongodb:6.0.4.RELEASE'
compile 'org.grails:grails-validation:3.2.3'
}
和域类,例如:
import grails.gorm.annotation.Entity
@Entity
class Module {
String id
String tags
}
GORM-ing由
Map config = new Yaml().load this.class.getResource( '/application.yml' ).text
new MongoDatastore( config, Module, UserAccount )
和域类的工作方式与在Grails应用程序中一样.
and the domain classes are working as they would in a Grails app.
2)一个Grails 3.2.3
应用:
dependencies {
// default grails dependencies
compile project( ':core-module' )
compile 'org.grails.plugins:mongodb:6.0.4'
compile 'org.grails.plugins:spring-security-core:3.1.1'
// etc
}
GORM初始化为:
def config = config.grails.mongodb
log.info "using $config"
new MongoDatastore( config, Module, UserAccount )
并将其打印到日志文件中:
and it prints out this into the log file:
g.app.init.com.mozaiq.Application-使用[host:localhost,port:27017,databaseName:theDB,username:theUN,pooled:true,mongoOptions:[connectionsPerHost:100,autoConnectRetry:true,connectTimeout: 3000]]
g.app.init.com.mozaiq.Application - using [host:localhost, port:27017, databaseName:theDB, username:theUN, pooled:true, mongoOptions:[connectionsPerHost:100, autoConnectRetry:true, connectTimeout:3000]]
问题在于,属性grailsApplication.domainClasses
为空,即使Module.collection
不为空,尽管集合不为空,但Module.count()
仍返回0
.
The problem is, that the property grailsApplication.domainClasses
is empty, and even though the Module.collection
is not-null, the Module.count()
return 0
despite the collection being not empty.
此外,在我的mongo客户端中,我看到在应用程序启动时,会创建一个新的数据库test
,其中包含一个空集合,该集合由我的一个域类命名.如果我在其中插入一些文档,则.count()
返回0
并且CRUD列表保持为空.
Also in my mongo-client I see that upon app start a new database test
is created with an empty collection, named by one of my domain classes. If I insert some documents into it, the .count()
returns 0
and the CRUD list remains empty.
推荐答案
出于性能原因,Grails默认仅扫描应用程序中的程序包.覆盖limitScanningToApplication()
以在Application
类中返回false
并通过覆盖Application
类中的packageNames()
来定义要扫描的软件包.
Grails only scans packages in the application by default for performance reasons. Override limitScanningToApplication()
to return false
in your Application
class and define the packages you wish to scan by overriding packageNames()
in your Application
class.
Grails随后将自动发现Mongo GORM类
Grails will then automatically discover the Mongo GORM classes
这篇关于将域类从GORM独立模块导入Grails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!