GORM 无法从插件中实现域 类 是 GORM 类

GORM fails to realize Domain classes from a plugin are GORM classes

我正在尝试使用 Grails 项目作为插件,以便基本上在插件中拥有我的域 classes,然后在多个 Grails 项目中使用它们。

我已经这样做了:

grails 创建应用程序 web

grails create-app 插件

在两个项目的根目录中创建一个 settings.gradle 包括 'plugin'、'web'

然后我向插件添加了 spring 安全性并使用 s2-quickstart 创建了一个用户和一个角色域 class 并向 Bootstrap.groovy 添加了一些默认用户。

单独启动插件项目不会出现任何问题。

现在我将插件添加为 Web 项目的依赖项: 编译(':插件') 这样我就可以从 Web 项目中的插件访问域 classes,它编译得很好。我将 spring 配置添加到 application.groovy,现在正尝试使用 Web 项目中插件中的域 classes。

尝试这个但是我的项目没有正确启动,它告诉我:

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

一旦我的代码尝试执行 new HtRole(...).save()

似乎插件中的域 classes 不知何故无法识别为 GORM classes。

域未被识别为 GORM class 的问题是由于其中提供的构造函数。这些构造函数是从 s2-quickstart 生成的,但应该被删除(这是 spring-security-core 中的一个错误)。我删除了构造函数和您使用它们的地方我使用了 map style default constructors。然后我修复了您必须获得当前用户的呼叫。

The repaired source is in this repo on GitHub(patch-1分支正在运行,master是OP的原始破损代码)

我在 Grails 3.1.6 中 运行 包含使用 grails run-app 的 GORM 域的插件时收到相同的错误消息。我通过提供初始化 Hibernate 的显式配置解决了这个问题,如下所示:

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