2 Getting Started - Reference Documentation
Authors: Graeme Rocher
Version: 5.0.8.RELEASE
Table of Contents
2 Getting Started
To use GORM 5.x for Hibernate 4 in Grails 3.x you can specify the following configuration inbuild.gradle:dependencies {
compile "org.grails.plugins:hibernate4:VERSION"
compile "org.hibernate:hibernate-ehcache"
}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"
}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"
}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")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 } }
UsingFinally create your GORM entities and ensure they are annotated withComponentScanwithout a value results in Boot scanning for classes in the same package or any package nested within theApplicationclass package. If your GORM entities are in a different package specify the package name as the value of theComponentScanannotation.
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"grails.gorm.annotation.Entity annotation:@Entity
class Person {
String name
}HibernateDatastoreSpringInitializer:def initializer = new HibernateDatastoreSpringInitializer(Person)
def applicationContext = initializer.configure()println Person.count()org.springframework.core.env.PropertyResolver interface:def initializer = new HibernateDatastoreSpringInitializer(['hibernate.log_sql':'true'], Person) def applicationContext = initializer.configure()println Person.count()
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()dataSource present.