Importing domain classes from GORM-standalone modu

2020-05-06 06:12发布

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条回答
你好瞎i
2楼-- · 2020-05-06 06:53

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

查看更多
登录 后发表回答