Login required
Download

GORM-JPA Plugin

(1)
Author(s): Graeme Rocher
Current Release: 0.5
Grails Version: 1.1 > *
Tags
This plugin replicates the behavior of the GORM Hibernate plugin by implementing most of the common API.

This plugin does not allow you to use GORM style mappings. You still have to use JPA annotations for your mappings. What it does provide is the runtime methods like save(), delete() and dynamic finders on top of JPA etc.

What is missing?

For the most part you can refer to the standard Grails reference documentation to find out what methods this plugin provides, however the following methods are not implemented due to limitations of the JPA API:

  • createCriteria
  • attach
  • discard
  • read
  • withCriteria
  • withSession
The following methods are also not implemented in this version yet because we just haven't got round to it:
  • countBy
  • listOrderBy

Using the Plugin

Note that this plugin doesn't actually setup a JPA EntityManagerFactory it relies on you or another plugin to configure one. All you need to do is setup the following two beans in your Spring application context:

  • EntityManagerFactory - The EntityManagerFactory is used to get hold of references to an EntityManager, you could use a Spring LocalContainerEntityManagerFactoryBean to set one up
  • JpaTransactionManager - To take advantage of declaration transactions and the rollback features of integration testing in Grails you need to setup a JpaTransactionManager
For example to do this in Hibernate you could configure the following beans in grails-app/conf/spring/resources.groovy:

entityManagerFactory(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean) {
    beanClassLoader = ref("classLoader")
}

transactionManager(org.springframework.orm.jpa.JpaTransactionManager) { entityManagerFactory = entityManagerFactory }

And then create a persistence.xml in web-app/WEB-INF/classes/META-INF as follows:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
   version="1.0">
   <persistence-unit name="manager">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <class>org.acme.Person</class>       
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      </properties>
   </persistence-unit>
</persistence>

Once these to beans are setup you can just install the plugin to get started:

grails install-plugin gorm-jpa

If you prefer to avoid all of this configuration there is a convenient hibernate-jpa-provider plugin that will do all of the above for you using sensible defaults so the installation is just:

grails install-plugin hibernate-jpa-provider
grails install-plugin gorm-jpa

The Google AppEngine plugin also sets up an EntityManager up automatically so all you need to do to get going is:

grails install-plugin app-engine
grails install-plugin gorm-jpa

Dynamic Finders and Criteria

Dynamic finders are supported by the plugin, but since JPA 1.0 doesn't support Criteria, criteria queries are not.