Grails 3.3.6是否未配置GORM?

时间:2018-08-09 14:34:45

标签: hibernate grails gorm grails3

我不得不将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
}

编辑:这是当我包含休眠依赖项时遇到的stacktrace错误:

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:

3 个答案:

答案 0 :(得分:0)

为什么您对休眠的依赖项被注释掉了?取消注释它们,或在必要时添加其他版本。

答案 1 :(得分:0)

我的项目是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"

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

然后在使用插件的应用程序中,我有一个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'
    }
}

请注意,gradle.properties中的gormVersion减去了“ .RELEASE”。在您的情况下,我想您必须减去“ .BUILD-SNAPSHOT”

最后,在常规项目依赖项中:

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

以及其他依赖项。

这在我的项目中工作正常。

答案 2 :(得分:0)

请确保您使用的是REALEASE版而不是BUILD-SNAPSHOT版,此问题已在v3.3.7中修复,因此您所要做的就是替换:

gormVersion=6.1.10.BUILD-SNAPSHOT

使用

gormVersion=6.1.10.RELEASE

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

希望这可以解决问题。