3

How do I make Grails 3.1.1 user Hibernate 5?

The following actions report Hibernate version 4.3.11.Final: In Grails 3.1.1

  1. grails create-app hello311
  2. edit BootStrap.groovy as shown below
  3. grails run-app

Console shows: Hibernate version is: 4.3.11.Final

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

My build.gradle is unedited. The create-app command resulted in the following build.gradle file:

    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
}

Changing the hibernate4 dependency to hibernate5 does not work.

DAC
  • 707
  • 1
  • 6
  • 20

2 Answers2

8

In build.gradle change the classpath dependency for the hibernate4 plugin at the top of the file in the buildscript{ dependencies {... section as follows:

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

The classpath section is there for the Gradle scrips like schemaExport, and that section does not support auto versioning.

Change the compile hibernate4 plugin dependency to the following:

compile "org.grails.plugins:hibernate5"

Add the following hibernate-core dependency:

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

Change the hibernate-ehcache dependency to the 5.0.7.Final version.

compile "org.hibernate:hibernate-ehcache:5.0.7.Final"
DAC
  • 707
  • 1
  • 6
  • 20
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
  • I did read the docs. They say to use Hibernate 5 in Grails 3.0.x the following config is needed... they also say that the resolutinoStrategy is not needed if using Grails 3.1 or above. Am I missing something? I read the docs to say that special configuration is not necessary in 3.1. – DAC Feb 04 '16 at 22:52
  • The `resolutionStrategy` block isn't needed, but you need to add the dependency for `hibernate-core` and specify the correct version for the existing `hibernate-ehcache` dependency because without those, the Grails/Gradle plugin that resolves versions when they're not specified uses 4.x versions, even though the Hibernate 5 plugin has correct dependencies – Burt Beckwith Feb 04 '16 at 22:55
  • OK, removing both dependencies for the hibernate plugin and adding a the hibernate-core dependency worked. Thanks Burt. – DAC Feb 04 '16 at 23:26
  • You still need the Hibernate plugin, otherwise you'll only have access to Hibernate, but not GORM – Burt Beckwith Feb 04 '16 at 23:27
  • OK, so then... remove the classpath hibernate4 dependency, change the compile hibernate4 plugin to hibernate5, add the 5.0.7.Final version to hibernate-ehcache, and add hibernate-core:5.0.7.Final. That seems to work. The hibernate5 plugin that it resolved to was version 5.0.1. Does that all sound right? – DAC Feb 05 '16 at 00:44
  • Yep, that's the most current. The `classpath` entry you removed from the `buildscript` block was there to make the plugin available to Gradle so scripts will work, in this case `schema-export`. Leave it out If you don't need that, or add it back, changing 4 to 5 and adding the version (5.0.1) since the auto-versioning stuff only works for the main `dependencies` block – Burt Beckwith Feb 05 '16 at 00:48
  • Oh, that's what that buildscript section is for! And that's why it won't work when I try to auto version it. Now it makes sense. – DAC Feb 05 '16 at 00:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102727/discussion-between-dac-and-burt-beckwith). – DAC Feb 05 '16 at 16:42
  • Hi Guys, I need to upgrate the Hibernate to Hibernate5 too in grails 3.1.1. I am following your answer but it does not work. I am getting the following error (I post it in parts because it does not fit in one comment): ERROR org.springframework.boot.SpringApplication - Application startup failed org.springframework.beans.factory.BeanCreationException: )V – Ectoras Feb 08 '16 at 17:32
  • Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.grails.orm.hibernate.cfg.CollectionType. – Ectoras Feb 08 '16 at 17:32
  • (Ljava/lang/Class;Lorg/grails/orm/hibernate/cfg/GrailsDomainBinder; – Ectoras Feb 08 '16 at 17:32
  • Hi, I think it should be `compile "org.grails.plugins:hibernate4"` instead of **hibernate5**. I got error with _hibernate5_ – sgiri Apr 07 '16 at 13:11
  • Sure, except that wasn't the question, and hibernate5 is a valid option – Burt Beckwith Apr 07 '16 at 14:28
0

In response to Ectoras comment on Burt's answer. Here is my full build.gradle file that works for me with 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
}
DAC
  • 707
  • 1
  • 6
  • 20
  • So we have run into this same problem. The only difference is that we are using version 5.0.8 for the plugin and 5.1.0.Final for the other hibernate dependencies. Anything else that may need to be changed? Works fine locally but not when deployed onto tcServer – Scott Jun 21 '16 at 20:33
  • I think you want the same exact versions on all Hibernate dependencies. But that's probably not your problem. Is the TC server that you are using a Java EE 6 or 7 container? Hibernate 5 is designed for EE 7. We were not able to get it to deploy to EE 6 servers. After much trying we eventually went back to Grails 2 and Hibernate 4 because of our EE 6 server. – DAC Jun 21 '16 at 23:53
  • We are actually using Java 8 – Scott Jun 22 '16 at 00:49
  • The JDK is the Java SE version - not the same thing. The issue is which version of the Java EE contract (API) is implemented by the server you are using? – DAC Jun 22 '16 at 18:24