【问题标题】:How to configure Grails 3.1.1 to use Hibernate 5如何配置 Grails 3.1.1 以使用 Hibernate 5
【发布时间】:2016-02-04 22:19:52
【问题描述】:

如何让 Grails 3.1.1 成为 Hibernate 5 用户?

以下操作报告 Hibernate 版本 4.3.11.Final: 在 Grails 3.1.1 中

  1. grails create-app hello311
  2. 如下所示编辑 BootStrap.groovy
  3. grails 运行应用程序

控制台显示: 休眠版本为:4.3.11.Final

class BootStrap {
    def init = { servletContext ->
        println "Hibernate version is: ${org.hibernate.Version.getVersionString()}"
    }
    def destroy = {}
}

我的 build.gradle 未经编辑。 create-app 命令生成以下 build.gradle 文件:

    buildscript {
    ext {
        grailsVersion = project.grailsVersion
    }
    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.5.0"
        classpath "org.grails.plugins:hibernate4:5.0.0"
    }
}

version "0.1"
group "hello311"

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

ext {
    grailsVersion = project.grailsVersion
    gradleWrapperVersion = project.gradleWrapperVersion
}

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

dependencyManagement {
    imports {
        mavenBom "org.grails:grails-bom:$grailsVersion"
    }
    applyMavenExclusions false
}

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:grails-dependencies"
    compile "org.grails:grails-web-boot"
    compile "org.grails.plugins:cache"
    compile "org.grails.plugins:scaffolding"
    compile "org.grails.plugins:hibernate4"
    compile "org.hibernate:hibernate-ehcache"
    console "org.grails:grails-console"
    profile "org.grails.profiles:web:3.1.1"
    runtime "org.grails.plugins:asset-pipeline"
    runtime "com.h2database:h2"
    testCompile "org.grails:grails-plugin-testing"
    testCompile "org.grails.plugins:geb"
    testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
    testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
}

task wrapper(type: Wrapper) {
    gradleVersion = gradleWrapperVersion
}

assets {
    minifyJs = true
    minifyCss = true
}

将 hibernate4 依赖改为 hibernate5 不起作用。

【问题讨论】:

    标签: hibernate grails grails-orm hibernate-5.x grails-3.1


    【解决方案1】:

    在 build.gradle 中,在 buildscript{ 依赖项 {... 部分的文件顶部更改 hibernate4 插件的类路径依赖项,如下所示:

    classpath "org.grails.plugins:hibernate5:5.0.1"
    

    类路径部分用于像 schemaExport 这样的 Gradle 脚本,并且该部分不支持自动版本控制。

    将编译hibernate4插件依赖改成如下:

    compile "org.grails.plugins:hibernate5"
    

    添加以下hibernate-core依赖:

    compile "org.hibernate:hibernate-core:5.0.7.Final"
    

    将hibernate-ehcache依赖改成5.0.7.Final版本。

    compile "org.hibernate:hibernate-ehcache:5.0.7.Final"
    

    【讨论】:

    • 我确实阅读了文档。他们说要在 Grails 3.0.x 中使用 Hibernate 5,需要以下配置……他们还说,如果使用 Grails 3.1 或更高版本,则不需要 resolutinoStrategy。我错过了什么吗?我阅读文档说 3.1 中不需要特殊配置。
    • 不需要resolutionStrategy 块,但您需要添加hibernate-core 的依赖项并为现有的hibernate-ehcache 依赖项指定正确的版本,因为没有这些,Grails/Gradle 插件在未指定版本时解析版本使用 4.x 版本,即使 Hibernate 5 插件具有正确的依赖关系
    • 好的,删除 hibernate 插件的两个依赖项并添加 hibernate-core 依赖项都有效。谢谢伯特。
    • 你仍然需要 Hibernate 插件,否则你只能访问 Hibernate,而不能访问 GORM
    • OK,那么...去掉classpath hibernate4依赖,把compile hibernate4插件改成hibernate5,把5.0.7.Final版本加到hibernate-ehcache,加hibernate-core:5.0。 7.决赛。这似乎行得通。它解析到的 hibernate5 插件是 5.0.1 版。听起来不错吗?
    【解决方案2】:

    回应 Ectoras 对 Burt 回答的评论。这是我的完整 build.gradle 文件,适用于 Hibernate 5。

    buildscript {
        ext {
            grailsVersion = project.grailsVersion
        }
        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.5.0"
            classpath "org.grails.plugins:hibernate5:5.0.1"
            classpath "org.grails.plugins:views-gradle:1.0.1"
        }
    }
    
    version "0.1"
    group "myGroup"
    
    apply plugin: "spring-boot"
    apply plugin: "war"
    apply plugin: "asset-pipeline"
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    apply plugin: "org.grails.grails-web"
    apply plugin: "org.grails.grails-gsp"
    apply plugin: "org.grails.plugins.views-json"
    
    ext {
        grailsVersion = project.grailsVersion
        gradleWrapperVersion = project.gradleWrapperVersion
    }
    
    assets {
        minifyJs = true
        minifyCss = true
    }
    
    repositories {
        mavenLocal()
        maven { url "https://repo.grails.org/grails/core" }
    }
    
    dependencyManagement {
        imports {
            mavenBom "org.grails:grails-bom:$grailsVersion"
        }
        applyMavenExclusions false
    }
    
    dependencies {
        compile "org.springframework.boot:spring-boot-starter-logging"
        compile "org.springframework.boot:spring-boot-starter-actuator"
        compile "org.springframework.boot:spring-boot-autoconfigure"
        compile "org.springframework.boot:spring-boot-starter-tomcat"
        compile "org.grails:grails-dependencies"
        compile "org.grails:grails-web-boot"
        compile "org.grails.plugins:cache"
        compile "org.grails.plugins:scaffolding"
    
        compile "org.grails.plugins:views-json:1.0.1"
    
        compile "org.grails.plugins:hibernate5"
        compile "org.hibernate:hibernate-core:5.0.7.Final"
        compile "org.hibernate:hibernate-ehcache:5.0.7.Final"
    
        console "org.grails:grails-console"
        profile "org.grails.profiles:web:3.1.1"
        runtime "org.grails.plugins:asset-pipeline"
    
        runtime files('grails-app/lib/ojdbc7.jar', 'grails-app/lib/xdb6.jar')
        compile files('grails/src/java')
    
        testCompile "org.grails:grails-plugin-testing"
        testCompile "org.grails.plugins:geb"
    
        // Note: It is recommended to update to a more robust driver (Chrome, Firefox etc.)
        testRuntime 'org.seleniumhq.selenium:selenium-htmlunit-driver:2.44.0'
    
        console "org.grails:grails-console"
    }
    
    task wrapper(type: Wrapper) {
        gradleVersion = gradleWrapperVersion
    }
    

    【讨论】:

    • 所以我们遇到了同样的问题。唯一的区别是我们对插件使用 5.0.8 版本,对其他休眠依赖项使用 5.1.0.Final。还有什么可能需要更改的吗?在本地工作正常,但在部署到 tcServer 时就不行
    • 我认为您希望所有 Hibernate 依赖项都具有相同的确切版本。但这可能不是你的问题。您使用的 TC 服务器是 Java EE 6 还是 7 容器? Hibernate 5 是为 EE 7 设计的。我们无法将其部署到 EE 6 服务器。经过多次尝试,由于我们的 EE 6 服务器,我们最终回到了 Grails 2 和 Hibernate 4。
    • 我们实际上是在使用 Java 8
    • JDK 是 Java SE 版本——不是一回事。问题是您使用的服务器实现了哪个版本的 Java EE 合同 (API)?
    猜你喜欢
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 2013-04-19
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多