GORM fails to realize Domain classes from a plugin

2020-03-24 18:36发布

问题:

I am trying to use a Grails Project as a Plugin to basically have my domain classes in the Plugin and then use them in multiple Grails projects.

I've done this:

grails create-app web

grails create-app plugin

create a settings.gradle in the root directory of both projects with include 'plugin', 'web'

then I added spring security to the plugin and used s2-quickstart to create a user and a role domain class and added some default users to the Bootstrap.groovy.

Starting the plugin project alone doesn't show any issues.

Now I added the plugin as a dependency to the web project: compile (':plugin') This way I can access the domain classes from the plugin inside the web project, it compiles fine. I added the spring config to the application.groovy and am now trying to use the domain classes from the plugin inside the web project.

Trying this however my project does not correctly start and it tells me this:

java.lang.IllegalStateException: Either class [htcommon.HtRole] is not a domain class or GORM has not been initialized correctly or has already been shutdown. If you are unit testing your entities using the mocking APIs

as soon as my code tries to do new HtRole(...).save()

It seems the domain classes from the plugin are not recognized as GORM classes somehow.

回答1:

The issue with the domain not being recognized as a GORM class was due to the constructors provided in them. These constructors were generated from s2-quickstart, but should be removed (it's a bug in spring-security-core). I removed the constructors and the one place you were using them I used map style default constructors. Then I fixed the call you had to get the current user.

The repaired source is in this repo on GitHub (patch-1 branch is working, master is the OP's original broken code)



回答2:

I received the same error message when running a plugin containing GORM domains using grails run-app in Grails 3.1.6. I fixed the problem by providing explicit configuration for initialising Hibernate as follows:

build.gradle:

dependencies {
    ...
    runtime "org.grails.plugins:hibernate4"
    runtime "org.hibernate:hibernate-ehcache"
}

grails-app/conf/application.yml:

---
environments:
    development:
        hibernate:
            cache:
                queries: false
                use_second_level_cache: true
                use_query_cache: false
                region.factory_class: 'org.hibernate.cache.ehcache.EhCacheRegionFactory'

         dataSource:
             pooled: true
             jmxExport: true
             driverClassName: org.h2.Driver
             username: sa
             password:
             dbCreate: create-drop
             url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE