Tuesday, 10 November 2009

Grails on GAE and persistence

In my last post we created very simple Grails project and deployed it on Google Application Engine. Now we will try to extend it to use Google Datastore for persistence.

Create a project in the same way as in previous post but ensure you will choose JPA as a persistence provider. Another option is JDO but I simply don't know this technology;)
Next step will be to install gorm-jpa plugin for Grails:
grails install-plugin gorm-jpa

Application goals
We will create very simple one page application to manage list of books. On the top there will be a list of book items. At the bottom there will a form to store new ones.

We should create class for our domain object - grails-app\domain\test\Book.groovy:
package test

import javax.persistence.*;

class Book implements Serializable {

@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id

String title

static constraints = {
id visible:false
We need also web controller that will be responsible for 3 web actions:
  • index - main action listing all books
  • save - adding new book
  • remove - removing book
Add following code to grails-app\controllers\BookController.groovy:
import test.*

class BookController {
def index = {
[books : Book.list( params )]

def save = {
Book b=new Book(params)
redirect(action : index)

def remove = {
Book b=Book.get(params.id)
redirect(action : index)
Finally add GSP page (grails-app\views\book\index.gsp) that will present data and invoke particular actions of our controller:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="main" />

<g:each in="${books}" status="i" var="book">
<td><g:link action="remove" id="${book.id}">remove</g:link> </td>
<g:form action="save">
<td><g:textField name="title" /></td>
<td colspan="2"><input type="submit" /></td>
Deploy the application just as we did it in last post. Point the browser to /book resource of your web app. You should see our page running.

Now, you can also browse datastore on GAE account pages:


  1. Very useful posts, thankyou, this has got me started with GAE.

  2. i think it won't work with on-to-many relationships when id type is long. you'd need to do the following

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    String id

    wojciech (your new colleague)