无法使用 Postgres DB 启动 Grails 3.3.8 应用程序

Unable to start Grails 3.3.8 application with Postgres DB

我开始在 Postgres DB 上将我的应用程序从 Grails 2.5.5 迁移到 Grails 3.3.8 运行,但在 IntelliJ 中启动应用程序时出现错误。以下是详细信息:

错误:

    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.NoSuchMethodError: org.hibernate.boot.internal.MetadataBuildingContextRootImpl.<init>(Lorg/hibernate/boot/spi/MetadataBuildingOptions;Lorg/hibernate/boot/spi/ClassLoaderAccess;Lorg/hibernate/boot/spi/InFlightMetadataCollector;)V
    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.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 com.mhz.tascr.Application.main(Application.groovy:8)
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.NoSuchMethodError

build.gradle:

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

repositories {
    mavenLocal()
    mavenCentral()

    maven { url "https://repo.grails.org/grails/core" }
    maven { url "http://repo.grails.org/grails/plugins/" }
}

dependencies {
    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.plugins:hibernate5"
    compile "org.hibernate:hibernate-core:5.3.6.Final"
    compile "org.hibernate:hibernate-ehcache:5.3.6.Final"

    runtime "postgresql:postgresql:9.0-801.jdbc4" // POSTGRES
}

application.yml:

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.postgresql.Driver
    dialect: org.hibernate.dialect.PostgreSQLDialect
environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:postgresql://localhost:5432/dev_test
            username: XXXX
            password: XXXX
            logSql: false

gradle.properties:

grailsVersion=3.3.8
gormVersion=6.1.10.RELEASE
gradleWrapperVersion=3.5

我已遵循 grails 迁移指南并提供了正确的依赖项和 Postgres 方言。我还在外部库中看到 postgresql-9.0-801.jdbc4.jar,因此无法找出错误原因。

谢谢

您使用的 jdbc 驱动程序是 2010 年的 - 使用较新的驱动程序,例如 https://mvnrepository.com/artifact/org.postgresql/postgresql/42.2.5

GORM 6.1 最高支持 Hibernate 5.2 - 您使用的是 Hibernate 5.3。

根据@erichelgeson 的建议,我升级了我的 jdbc 驱动程序,但还必须进行一些其他更改,例如添加 org.jadira.usertype:usertype.core:5.0.0.GA 并将 region.factory_class 更改为 org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory 让它完全工作。

build.gradle:

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

repositories {
    mavenLocal()
    mavenCentral()

    maven { url "https://repo.grails.org/grails/core" }
    maven { url "http://repo.grails.org/grails/plugins/" }
}

dependencies {
    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.plugins:hibernate5:${gormVersion-".RELEASE"}"
    compile "org.hibernate:hibernate-ehcache"
    compile "org.jadira.usertype:usertype.core:5.0.0.GA"

    runtime "org.postgresql:postgresql:42.2.5" // POSTGRES
}

application.yml:

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