【问题标题】:Grails 3.3.6 Does not come with GORM Configured?Grails 3.3.6 没有配置 GORM?
【发布时间】:2018-08-09 14:34:44
【问题描述】:

我不得不将 grails 插件从 2.3.11 升级到 3.3.6。当我测试完成的端口时,我注意到我的 GORM 查询有错误。如果我记得,在 grails 2 中,GORM 已经附带了它。在 grails 3.3.6 中不是这种情况吗?我需要做什么才能启用 GORM?我会提到我正在使用 SQL Server 数据源。这是我从 grails 控制台执行简单的Person.first()

得到的错误
Exception thrown

java.lang.IllegalStateException: Either class [comp.Person] 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:469)

    at org.grails.datastore.gorm.GormEnhancer.findStaticApi(GormEnhancer.groovy:300)

    at org.grails.datastore.gorm.GormEnhancer.findStaticApi(GormEnhancer.groovy:296)

    at org.grails.datastore.gorm.GormEntity$Trait$Helper.currentGormStaticApi(GormEntity.groovy:1368)

    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1427)

    at comp.Person.currentGormStaticApi(Person.groovy)

    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1427)

    at org.grails.datastore.gorm.GormEntity$Trait$Helper.first(GormEntity.groovy:720)

    at org.grails.datastore.gorm.GormEntity$Trait$Helper$first.call(Unknown Source)

    at comp.Person.first(Person.groovy)

    at comp.Person$first.call(Unknown Source)

    at ConsoleScript1.run(ConsoleScript1:3)

    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1427)

在我的gradle.properties 文件中写着gormVersion=6.1.10.BUILD-SNAPSHOT

这是我的build.gradle

buildscript {
    repositories {
        mavenLocal()
        maven { url "https://repo.grails.org/grails/core" }
    }
    dependencies {
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
        classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.14.2"

    }
}

version "2.0-SNAPSHOT"
group "pluginmigration"

apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"org.grails.grails-plugin"
apply plugin:"org.grails.grails-plugin-publish"
apply plugin:"asset-pipeline"
apply plugin:"org.grails.grails-gsp"

repositories {
    mavenLocal()
    maven { url "https://repo.grails.org/grails/core" }
}

dependencies {

    //compile "org.grails.plugins:hibernate5:6.1.10"
    //compile "org.hibernate:hibernate-ehcache"
    compile 'org.grails.plugins:spring-security-core:3.2.3'
    compile 'org.grails.plugins:spring-security-ldap:3.0.2'
    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-autoconfigure"
    compile "org.grails:grails-core"
    compile "org.springframework.boot:spring-boot-starter-actuator"
    compile "org.springframework.boot:spring-boot-starter-tomcat"
    compile "org.grails:grails-web-boot"
    compile "org.grails:grails-logging"
    compile "org.grails:grails-plugin-rest"
    compile "org.grails:grails-plugin-databinding"
    compile "org.grails:grails-plugin-i18n"
    compile "org.grails:grails-plugin-services"
    compile "org.grails:grails-plugin-url-mappings"
    compile "org.grails:grails-plugin-interceptors"
    compile "org.grails.plugins:cache"
    compile "org.grails.plugins:async"
    compile "org.grails.plugins:scaffolding"
    compile "org.grails.plugins:gsp"
    console "org.grails:grails-console"
    profile "org.grails.profiles:web-plugin"
    provided "org.grails:grails-plugin-services"
    provided "org.grails:grails-plugin-domain-class"
    runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.14.2"
    runtime 'net.sourceforge.jtds:jtds:1.3.1'
    testCompile "org.grails:grails-gorm-testing-support"
    testCompile "org.grails:grails-plugin-testing"
    testCompile "org.grails:grails-web-testing-support"
}

bootRun {
    jvmArgs('-Dspring.output.ansi.enabled=always')
    addResources = true
    String springProfilesActive = 'spring.profiles.active'
    systemProperty springProfilesActive, System.getProperty(springProfilesActive)
}

// enable if you wish to package this plugin as a standalone application
bootRepackage.enabled = false
grailsPublish {
    // TODO: Provide values here
    user = 'user'
    key = 'key'
    githubSlug = 'foo/bar'
    license {
        name = 'Apache-2.0'
    }
    title = "My Plugin"
    desc = "Full plugin description"
    developers = [johndoe:"John Doe"]
}



assets {
    packagePlugin = true
}

编辑:这是我在包含休眠依赖项时得到的堆栈跟踪错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'methodValidationPostProcessor' defined in class path resource [org/springframework/boot/autoconfigure/validation/
ValidationAutoConfiguration.class]: Unsatisfied dependency expressed through method 'methodValidationPostProcessor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastoreServiceRegistry': Cannot resolve reference to bean 'hibernateDatastore' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate
 [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1181)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1075)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:225)
        at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:703)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:528)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
        at grails.boot.GrailsApp.run(GrailsApp.groovy:84)
        at grails.boot.GrailsApp.run(GrailsApp.groovy:393)
        at grails.boot.GrailsApp.run(GrailsApp.groovy:380)
        at grails.boot.GrailsApp$run.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136)
        at pluginmigration.Application.main(Application.groovy:7)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastoreServiceRegistry': Cannot resolve reference to bean 'hibernateDatastore' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Bean instantiation via constructor failed; nested exception is org
.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
        at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:648)
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:145)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1201)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1103)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:931)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:808)
        at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:564)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:432)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:395)
        at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:206)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1267)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
        at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
        ... 24 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Bean instantiation via constructor failed; nested exception is org.springframework.beans.B
eanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1201)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1103)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
        ... 40 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is java.lang.NullPointerExcept
ion
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122)
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:271)
        ... 49 common frames omitted
Caused by: java.lang.NullPointerException: null
        at java.util.Hashtable.put(Hashtable.java:460)
        at org.grails.orm.hibernate.connections.HibernateConnectionSourceSettings$HibernateSettings.populateProperties(HibernateConnectionSourceSettings.groovy:258)
        at org.grails.orm.hibernate.connections.HibernateConnectionSourceSettings$HibernateSettings.populateProperties(HibernateConnectionSourceSettings.groovy:255)
        at org.grails.orm.hibernate.connections.HibernateConnectionSourceSettings$HibernateSettings.populateProperties(HibernateConnectionSourceSettings.groovy:255)
        at org.grails.orm.hibernate.connections.HibernateConnectionSourceSettings$HibernateSettings.toProperties(HibernateConnectionSourceSettings.groovy:246)
        at org.grails.orm.hibernate.connections.HibernateConnectionSourceSettings$HibernateSettings$toProperties.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
        at org.grails.orm.hibernate.connections.HibernateConnectionSourceSettings.toProperties(HibernateConnectionSourceSettings.groovy:51)
        at org.grails.orm.hibernate.connections.HibernateConnectionSourceFactory.buildConfiguration(HibernateConnectionSourceFactory.java:242)
        at org.grails.orm.hibernate.connections.HibernateConnectionSourceFactory.create(HibernateConnectionSourceFactory.java:85)
        at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:39)
        at org.grails.orm.hibernate.connections.AbstractHibernateConnectionSourceFactory.create(AbstractHibernateConnectionSourceFactory.java:23)
        at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:64)
        at org.grails.datastore.mapping.core.connections.AbstractConnectionSourceFactory.create(AbstractConnectionSourceFactory.java:52)
        at org.grails.datastore.mapping.core.connections.ConnectionSourcesInitializer.create(ConnectionSourcesInitializer.groovy:24)
        at org.grails.orm.hibernate.HibernateDatastore.<init>(HibernateDatastore.java:204)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.springsource.loaded.ri.ReflectiveInterceptor.jlrConstructorNewInstance(ReflectiveInterceptor.java:1076)
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
        ... 51 common frames omitted

EDIT2:根据要求,我在application.yml 中的数据源块已包含在内(也有休眠以防万一):

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


  environments:
    development:
      dataSources:
        one:
          dialect: org.hibernate.dialect.SQLServer2008Dialect
          logSql: true
          formatSql: true
          driverClassName: net.sourceforge.jtds.jdbc.Driver
          dbCreate: none
          url: jdbc:jtds:sqlserver://localhost:1804/one
          username: u
          password: p

        two:
          dialect: org.hibernate.dialect.SQLServer2008Dialect
          logSql: true
          formatSql: true
          driverClassName: net.sourceforge.jtds.jdbc.Driver
          dbCreate: none
          url: jdbc:jtds:sqlserver://localhost:1804/two
          username: u
          password: p

        three:
          dialect: org.hibernate.dialect.SQLServer2008Dialect
          logSql: true
          formatSql: true
          driverClassName: net.sourceforge.jtds.jdbc.Driver
          dbCreate: none
          url: jdbc:jtds:sqlserver://localhost:1804/three
          username: u
          password: p

    test:
      dataSources:
    production:
      dataSources:

【问题讨论】:

  • 你的 GORM Hibernate5 插件被注释掉了。如果您不使用那个,则需要从 gorm.grails.org 中选择其他实现之一。
  • 取消注释这些行时出现堆栈跟踪错误。
  • 您的堆栈跟踪是因为您的数据源存在问题。显示您的环境 { development { 来自 application.groovy 或 application.yml 的数据源块。
  • @Daniel 我的数据源在 application.yml 中。但是我也有 application.groovy(这个插件是从 grails 2 迁移而来的)。这有关系吗?
  • @Daniel 我添加了数据源。

标签: hibernate grails grails-orm grails3


【解决方案1】:

请确保您使用的是REALEASEversion 而不是BUILD-SNAPSHOT,此问题已在v3.3.7 中修复,因此您只需替换:

gormVersion=6.1.10.BUILD-SNAPSHOT

gormVersion=6.1.10.RELEASE

在您的 gradle.properties 文件中,然后再次重建项目。

希望这能解决问题。

【讨论】:

    【解决方案2】:

    为什么你对hibernate的依赖被注释掉了?如有必要,取消注释或添加其他版本。

    【讨论】:

    • Daniel,在包含休眠依赖项时出现堆栈跟踪错误:
    【解决方案3】:

    我的项目是 Grails 3.3.5

    grailsVersion=3.3.5
    grailsWrapperVersion=1.0.0
    gormVersion=6.1.8.RELEASE
    gradleWrapperVersion=3.5
    

    所以你需要进行版本调整,但是...

    我注意到您的构建文件是用于 grails 插件的。在我的插件项目中,设置如下:

    # Note: no hibernate related dependencies in the 'compile' configuration
    
    testCompile "org.grails:grails-gorm-testing-support"
    testCompile "org.grails:grails-plugin-testing"
    testCompile "org.grails:grails-web-testing-support"
    testCompile "org.grails.plugins:hibernate5"
    testCompile "org.hibernate:hibernate-core:5.1.5.Final"
    

    注意,hibernate 插件上没有版本说明符。

    然后在使用我的插件的应用程序中,我有一个 buildscript 块:

    buildscript {
        repositories {
            mavenLocal()
            maven {
                url "https://repo.grails.org/grails/core"
            }
            jcenter()
            mavenCentral()
        }
        dependencies {
            classpath "org.grails:grails-gradle-plugin:$grailsVersion"
            classpath "gradle.plugin.com.energizedwork.webdriver-binaries:webdriver-binaries-gradle-plugin:1.4"
            classpath "gradle.plugin.com.energizedwork:idea-gradle-plugins:1.4"
            classpath "org.grails.plugins:hibernate5:${gormVersion - ".RELEASE"}"
            classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.14.6"
            classpath 'org.grails.plugins:database-migration:3.0.3'
        }
    }
    

    请注意,“.RELEASE”是从 gradle.properties 中的 gormVersion 中减去的。在你的情况下,我猜你必须减去“.BUILD-SNAPSHOT”

    最后在我拥有的常规项目依赖项中:

    compile "org.grails.plugins:hibernate5"
    compile "org.hibernate:hibernate-core:5.1.5.Final"
    

    连同我的其他依赖项。

    这在我的项目中运行良好。

    【讨论】:

    • 你真的在你的插件中使用了 GORM 命令吗?这就是我正在做的事情。它不工作。除了 grails 版本之外,我还复制了您的设置。我不知道你在插件中使用了什么数据库(如果有的话),但我有 sql server setup (runtime 'net.sourceforge.jtds:jtds:1.3.0')
    • 插件确实与数据库交互(使用 gorm),它包含域对象和服务。在测试中我使用 H2,在生产中我使用 MySQL。
    猜你喜欢
    • 2013-11-13
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多