Importing domain classes from GORM-standalone module into Grails

三世轮回 提交于 2019-12-20 07:15:40

问题


I have 2 pieces of my puzzle:

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

and domain classes like:

import grails.gorm.annotation.Entity

@Entity
class Module {
  String id
  String tags
 }

the GORM-ing is initialized by

Map config = new Yaml().load this.class.getResource( '/application.yml' ).text
new MongoDatastore( config, Module, UserAccount )

and the domain classes are working as they would in a Grails app.

2) a Grails 3.2.3 app:

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
}

the GORM is initialized so:

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 - using [host:localhost, port:27017, databaseName:theDB, username:theUN, pooled:true, mongoOptions:[connectionsPerHost:100, autoConnectRetry:true, connectTimeout:3000]]

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.

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.


回答1:


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 will then automatically discover the Mongo GORM classes



来源:https://stackoverflow.com/questions/41143475/importing-domain-classes-from-gorm-standalone-module-into-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!