【问题标题】:grails 3.2.6 fails to initialise test data "is not a domain class or GORM has not been initialized correctly"grails 3.2.6 无法初始化测试数据“不是域类或 GORM 未正确初始化”
【发布时间】:2017-07-01 20:03:31
【问题描述】:

我用最新的 v3.2.6 创建了一个简单的 grails 应用程序。在 grails-app/domain 中创建了 Authors 和 Book 类。

grails-app/domain 中的 book 类看起来像这样(我也有一个 books 类)

package org.softwood

//Author has many books logical model

class Author {

    String name
    Collection books

    //static hasMany = [books:Book]

    static constraints = {
        books nullable:true
    }
}

并为完整性预订域类

package org.softwood

class Book {

    String title
    Author author

    //static belongsTo = [author:Author]

    static constraints = {
        author nullable:true
    }
}

我启动了应用程序 - 没有错误,并尝试使用 grails 控制台创建 Author 的实例并收到类似这样的错误

java.lang.IllegalStateException: Either class [org.softwood.Author] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.

所以我编辑了 BootStrap.groovy 以尝试像这样在启动时创建一个。为什么它没有正确调出 Gorm?

import org.softwood.*

class BootStrap {

    def init = { servletContext ->
        environments {
            development{
                createDevData()
            }
            test {}
            production {}
        }
    }
    def destroy = {
    }

    def createDevData() {
        Author a = new Author(name:"will")
        def res = a.save (flush:true, failOnExit:true)
        assert res
        assert Author.get(1).name == "will"
    }
}

当我使用相同的消息启动应用程序时出现此错误,堆栈跟踪如下

java.lang.IllegalStateException: Either class [org.softwood.Author] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.
    at org.grails.datastore.gorm.GormEnhancer.stateException(GormEnhancer.groovy:387)
    at org.grails.datastore.gorm.GormEnhancer.findInstanceApi(GormEnhancer.groovy:273)
    at org.grails.datastore.gorm.GormEnhancer.findInstanceApi(GormEnhancer.groovy:270)
    at org.grails.datastore.gorm.GormEntity$Trait$Helper.currentGormInstanceApi(GormEntity.groovy:1326)
    at org.grails.datastore.gorm.GormEntity$Trait$Helper.save(GormEntity.groovy:151)
    at org.grails.datastore.gorm.GormEntity$Trait$Helper$save.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at org.softwood.Author.save(Author.groovy)
    at org.softwood.Author.save(Author.groovy)
    at org.grails.datastore.gorm.GormEntity$save.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at test.BootStrap.createDevData(BootStrap.groovy:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:384)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:158)
    at test.BootStrap$_closure1$_closure3$_closure4.doCall(BootStrap.groovy:10)
    at test.BootStrap$_closure1$_closure3$_closure4.doCall(BootStrap.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
    at groovy.lang.Closure.call(Closure.java:414)
    at groovy.lang.Closure.call(Closure.java:408)
    at grails.util.Environment$EnvironmentBlockEvaluator.execute(Environment.java:529)
    at grails.util.Environment.executeForEnvironment(Environment.java:510)
    at grails.util.Environment.executeForCurrentEnvironment(Environment.java:485)
    at org.grails.web.servlet.boostrap.DefaultGrailsBootstrapClass.callInit(DefaultGrailsBootstrapClass.java:62)
    at org.grails.web.servlet.context.GrailsConfigUtils.executeGrailsBootstraps(GrailsConfigUtils.java:65)
    at org.grails.plugins.web.servlet.context.BootStrapClassRunner.onStartup(BootStrapClassRunner.groovy:53)
    at grails.boot.config.GrailsApplicationPostProcessor.onApplicationEvent(GrailsApplicationPostProcessor.groovy:256)
    at grails.boot.config.GrailsApplicationPostProcessor.onApplicationEvent(GrailsApplicationPostProcessor.groovy)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:372)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:83)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:388)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:375)
    at grails.boot.GrailsApp$run.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at test.Application.main(Application.groovy:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

【问题讨论】:

  • 奇怪的小精灵。在等待时 - 因损坏而报废并再次重新创建一个全新的应用程序。重新创建了作者/书籍部分并作为集成测试重新运行 - 它有效,进入控制台并在交互式控制台上输入 - 它有效。所以我不知道为什么以前的全新安装不起作用 - 但显然坏了。很奇怪
  • 您的域类是否与您的Application 类在同一个包中?如果不是,您可能需要覆盖packageNames()
  • 不,它们不是——默认的应用程序 groovy 在包“package coffeeshopapp”中——与项目相同。域类在“package org.softwood”中。正如我提到的那样,在 intellij 中进行 gradle clean 似乎又可以修复了 - 从命令行运行应用程序也是如此。你是什​​么意思覆盖“packageNames()”不知道你的意思是什么

标签: grails grails-orm


【解决方案1】:

试试这个

package org.softwood

//Author has many books logical model

class Author {

    String name
    //Collection books

    static hasMany = [books:Book]

    static constraints = {
        books nullable:true    // you may not need this.
    }
}

【讨论】:

  • 看起来不像是 grails - 当我在命令行上使用 run-app 时它工作正常,当我在 intellij 中尝试 gradle clean 时它又开始工作了。似乎是 intellij 失步和生成停止正常工作的问题。运行干净的动作会保持在它之上 - 但我认为不应该真的发生
猜你喜欢
  • 2018-08-03
  • 1970-01-01
  • 1970-01-01
  • 2022-08-11
  • 1970-01-01
  • 2013-04-03
  • 2015-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多