(Quick Reference)

2 Getting Started - Reference Documentation

Authors: Graeme Rocher

Version: 5.0.8.RELEASE

2 Getting Started

To use GORM 5.x for Hibernate 4 in Grails 3.x you can specify the following configuration in build.gradle:

dependencies {
    compile "org.grails.plugins:hibernate4:VERSION"
    compile "org.hibernate:hibernate-ehcache"
}

Where VERSION is 5.0.0 or above.

2.1 Configuring Different Hibernate Versions

To use Hibernate 5 in Grails 3.0.x the following configuration is needed:

// the below is unnecessary in Grails 3.1 and above, but required in Grails 3.0.x
configurations.all {
    resolutionStrategy {
        eachDependency { DependencyResolveDetails details ->
            if(details.requested.group == 'org.springframework') {
                details.useVersion('4.2.3.RELEASE')
            }
        }
    }
}

// the following is necessary in all versions of Grails 3 dependencies { compile "org.grails.plugins:hibernate5:$gormVersion" compile "org.hibernate:hibernate-core:5.1.0.Final" compile "org.hibernate:hibernate-ehcache:5.1.0.Final" }

The resolutionStrategy is needed to enforce an upgrade to Spring 4.2.x which is required by Hibernate 5 support. This block is not needed if you are using Grails 3.1 or above.

To use Hibernate 3 the following configuration is needed:

dependencies {
    compile "org.grails.plugins:hibernate3:VERSION"
    compile "org.hibernate:hibernate-core:3.6.10.Final"
    compile "org.hibernate:hibernate-ehcache:3.6.10.Final"
}

For Grails 2.x only the Hibernate 4 plugin is supported and can be added with the following configuration in BuildConfig.groovy:

plugins {
 compile ':hibernate4:VERSION'
}

2.2 Using GORM in Spring Boot

To use GORM for Hibernate in Spring Boot add the necessary dependencies to your Boot application:

compile("org.grails:gorm-hibernate4-spring-boot:VERSION")

Ensure your Boot Application class is annotated with ComponentScan, for example:

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.*

@Configuration @EnableAutoConfiguration @ComponentScan class Application { static void main(String[] args) { SpringApplication.run Application, args } }

Using ComponentScan without a value results in Boot scanning for classes in the same package or any package nested within the Application class package. If your GORM entities are in a different package specify the package name as the value of the ComponentScan annotation.

Finally create your GORM entities and ensure they are annotated with grails.persistence.Entity:

import grails.persistence.*

@Entity class Person { String firstName String lastName }

2.3 Using GORM for Hibernate Outside Grails

If you wish to use GORM for Hibernate outside of a Grails application you should declare the necessary dependencies, for example in Gradle:

compile "org.grails:grails-datastore-gorm-hibernate4:VERSION"

Then annotate your entities with the grails.gorm.annotation.Entity annotation:

@Entity
class Person {
    String name
}

Then you need to place the bootstrap logic somewhere in your application which uses HibernateDatastoreSpringInitializer:

def initializer = new HibernateDatastoreSpringInitializer(Person)
def applicationContext = initializer.configure()

println Person.count()

For configuration you can either pass a map or an instance of the org.springframework.core.env.PropertyResolver interface:

def initializer = new HibernateDatastoreSpringInitializer(['hibernate.log_sql':'true'], Person)
def applicationContext = initializer.configure()

println Person.count()

If you are using Spring with an existing ApplicationContext you can instead call configureForBeanDefinitionRegistry prior to refreshing the context. You can pass the Spring Environment object to the constructor for configuration:

ApplicationContext myApplicationContext = …
def initializer = new HibernateDatastoreSpringInitializer(myApplicationContext.getEnvironment(), Person)
initializer.configureForBeanDefinitionRegistry(myApplicationContext)

println Person.count()

Note in this case GORM expects there to be a bean named dataSource present.