Update The examples now point to a local snapshot repository of SimpleJPA as the current 1.6 version has quite a few bugs related to Groovy domain classes. Using this version will ensure you get the most recent set of fixes committed for Groovy support.
This post assumes a very basic knowledge of Grails, like that provided by the standard Quick Start Tutorial. It also assumes that you have a valid Amazon AWS account with SimpleDB enabled, and that you know your AWS access credentials.
Step 1: Create a new Grails app
In this example case, we will make a trivial library app to track books.
grails create-app libraryStep 2: Add the gorm-jpa plugin and remove the preconfigured hibernate plugin
Grails will default to using Hibernate for persistence within GORM. We will be using simplejpa for persistence rather than hibernate and hibernate-jpa.
grails install-plugin gorm-jpa grails uninstall-plugin hibernateStep3: Pull in SimpleJPA Dependencies
Unfortunately artifacts from the SimpleJPA google code project aren’t available in a common repository, but you can add it to your local maven or ivy repository, or you can pull it from the third party repository from this site. Update the BuildConfig.groovy to include the appropriate Maven Repositories and add dependencies on STAX and SimpleJPA. The underlying AWS Java API needs a recent STAX build, but stax-api must explicitly be excluded to avoid core library conflicts with Grails
resources.groovy:
repositories {
grailsPlugins()
grailsHome()
grailsCentral()
mavenLocal()
mavenCentral()
mavenRepo "http://repository.overcomplified.com/content/repositories/thirdpartysnapshots/"
}
dependencies {
runtime('SimpleJPA:SimpleJPA:1.6-SNAPSHOT'){ excludes 'stax-api' }
runtime('stax:stax:1.2.0'){ excludes 'stax-api' }
}
Step4: Configure SimpleJPA and and GORM
GORM-JPA will require entityManager and transactionManager spring beans. Update to your grails-app/conf/spring/resources.groovy file, declaring SimpleJPA as your entityManager and JPA as your transaction provider
resources.groovy:
beans = {
entityManagerFactory(com.spaceprogram.simplejpa.EntityManagerFactoryImpl, "library", null) {}
transactionManager(org.springframework.orm.jpa.JpaTransactionManager) {
entityManagerFactory = entityManagerFactory
}
}
Step 5: Configure your AWS Credentials
Add a simplejpa.propeties file to your grails-app/conf directory. It should contain your AWS credentials, and include the groovyBeans=true line to allow GORM-JPA domain classes to be handled correctly
simplejpa.properties:
accessKey=<your access key>
secretKey=<your secret key>
printQueries=true
groovyBeans=true
Step 6: Create a simple domain class and controller
grails create-domain-class com.overcomplified.Book grails create-controller com.overcomplified.BookStep 7: Add JPA annotations and fields to your Book class
Normal GORM managed domain classes don’t need any annotations, but since we are using gorm-jpa, all domain classes must be annotated using the appropriate javax.persistence.* annotations.
Book.groovy:
package com.overcomplified
import javax.persistence.*
@Entity
class Book {
@Id
String id // ID fields in SimpleDB should be Strings
String isbn
String title
String author
String publisher
Date publishDate
}
Step 7: Configure the Controller for Scaffolding
The simplest way to add CRUD components to a demo application is through Grails’ great scaffolding mechanism. To prove that our application works, we will use this default scaffolding to test the CRUD methods on our Book domain class.
BookController.groovy:
package com.overcomplified
class BookController {
def scaffold = Book
}
Step 8: Run the App
Start the application with grails run-app. You should be able to use the scaffolded controller and add/view new Books. Note that there is an issue with gorm-jpa that will cause all fields to be presented as ‘Book’ rather than their field name in the UI. This can be easily remedied by creating your own GSP views.
Average cardinalities for each relationship are noted below the role names in the diagram.


