Last updated by 5 years ago

Page: Developer - Hibernate Integration, Version:0

Hibernate Integration

Configuration

Grails uses Hibernate for ORM, but implements it's own configuration strategy for Hibernate to use convention to perform ORM mapping. The result is GORM. The core Hibernate integration classes can be found within the org.codehaus.groovy.grails.orm.hibernate directory. Within this package resides a Spring factory bean called ConfigurableLocalSessionFactoryBean that extends the Spring factory bean for creating Hibernate session factories allowing a custom hibernate Configuration class to be set.

Grails then provides its own configuration class within the org.codehaus.groovy.grails.orm.hibernate.cfg package called DefaultGrailsDomainConfiguration this class extends the Hibernate Configuration the implication of which means it will still read in a regular hibernate.cfg.xml file if you want to use Hibernate XML mapping instead.

Grails provides a second Configuration class called GrailsAnnotationConfiguration that extends the Hibernate AnnotationConfiguration class to allow EJB3 annotation mapping in Grails.

Both configuration classes delegate to a class called org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder that performs the object relational mapping. It "binds" the Grails domain class conventions to the Hibernate meta model.

Persistence Methods

The dynamic methods that magically appear on domain classes such as "save" and "delete" can be found in the package org.codehaus.groovy.grails.orm.hibernate.metaclass. They use the architecture described in the section on Dynamic Method Injection.

The class which these methods are added to is called org.codehaus.groovy.grails.metaclass.DomainClassMethods so essentially adding a new method to a domain class would involve creating a new one in the aformentioned package and adding a line such as:

addDynamicMethodInvocation(new MyBrandNewMethod(sessionFactory, classLoader,application));

Or for static methods:

addStaticMethodInvocation(new MyNewStaticMethod(sessionFactory, classLoader));