Last updated by tomnelson 2 years ago

Using Gorm outside of Grails (GORM)

It is possible to use GORM outside of Grails but note that Spring is required to do so. See the "petclinic-mvc" sample application in the "grails-samples" project https://github.com/grails-samples/petclinic on Github for an example of using GORM in a plain Spring MVC application.

The key is the configuration of a GORM enhanced Hibernate SessionFactory as found in the web/WEB-INF/applicationContext.xml file:

<gorm:sessionFactory base-package="org.grails.samples" data-source-ref="dataSource"
	                     message-source-ref="messageSource">
        <property name="hibernateProperties">
            <util:map>
                <entry key="hibernate.hbm2ddl.auto" value="update"/>
            </util:map>
        </property>
    </gorm:sessionFactory>

The above uses a Spring namespace to setup GORM. The GORM sessionFactory bean needs references to the package where you GORM domain classes live ("org.grails.samples" in this example), the name of the dataSource bean and the name of the messageSource bean.

You also need to add import grails.persistence.Entity and @Entity to your domain classes. This can be automated after running "grails install-templates" by editing src/templates/artifacts/DomainClass.groovy as follows:

@artifact.package@
import grails.persistence.Entity

@Entity class artifact.name {

static constraints = { } }

If you want to use GORM events such as beforeInsert, afterDelete etc. you need to add the following property to your gorm:sessionFactory bean:

<property name="eventListeners">
  <map>
    <entry key="flush">
      <bean
        class="org.codehaus.groovy.grails.orm.hibernate.events.PatchedDefaultFlushEventListener" />
    </entry>
    <entry key="pre-load">
      <bean
        class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor" />
    </entry>
    <entry key="post-load">
      <bean
        class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor" />
    </entry>
    <entry key="save">
      <bean
        class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor" />
    </entry>
    <entry key="save-update">
      <bean
        class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor" />
    </entry>
    <entry key="post-insert">
      <bean
        class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor" />
    </entry>
    <entry key="pre-update">
      <bean
        class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor" />
    </entry>
    <entry key="post-update">
      <bean
        class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor" />
    </entry>
    <entry key="pre-delete">
      <bean
        class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor" />
    </entry>
    <entry key="post-delete">
      <bean
        class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor" />
    </entry>
  </map>
</property>