Introduction

GORM for Hibernate is the original implementation of GORM and has evolved dramatically over the years from a few meta-programming functions into a complete data access framework with multile implementations for different datastores relational and NoSQL.

The full documentation for GORM for Hibernate can be found in the Grails User Guide, this documentation serves to explain how to get started with GORM for Hibernate with or without Grails and to document release notes.

Upgrade Notes

Since GORM 5, GORM for Hibernate has been rewritten to support Hibernate 3, 4 and 5 and also to support both Grails 2.x and Grails 3.x.

To make this possible the internals of GORM have been heavily refactored and some common previously used classes replaced.

Below is a list of removed or deprecated classes and appropriate replacements:

  • org.grails.orm.hibernate.ConfigurableLocalSessionFactoryBean - Replaced by org.grails.orm.hibernate.HibernateMappingContextSessionFactoryBean

  • org.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration ` - Replaced by `org.grails.orm.hibernate.HibernateMappingContextConfiguration

In addition GORM used to use meta-programming to enhance classes, now traits and compile time AST transformations are used exclusively. This means that support for Java entities and JPA entities have been removed. If you need those features you can use a version prior to the 5.x release.

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.

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'
}

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
}

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.

Learning More

The full documentation for GORM for Hibernate can be found in the Grails User Guide, this documentation serves to explain how to get started with GORM for Hibernate with or without Grails and to document release notes.