Grails - associations

After going through the Grails documentation I decided to get my hands dirty and actually try and build something. 'Todo' style applications aren't too interesting, but quite useful to test basic stuff.

Getting started
After installing Grails I moved on to creating a new application:

grails create-app ptodo

This creates a fully working webapplication, very similar to the basic Rails command. Directories are created for configuration, controllers, domain objects, tags, tests, plugins and much more.

The application can be started from the generated directory:

grails run-app

Which starts the build-in server for debugging purposes.

Model
By running:

grails create-domain-class

One can generate the basic domain classes used in the application. Doing so will create skeleton classes in the domain directory of the project and counterpart test files.

Lets get started in create two classes in our model Task and User, which have a one to many relationship via the owner attribute of Task:

Model

Grails' object relational mapping (GORM) proves to be quite nice. Its' elegant syntax makes creating a model a breeze, whilst you can always fall back on the underlying Hibernate framework.

User.groovy:

GROOVY:
  1. class User {
  2.   String name
  3.   String email
  4.  
  5.   static hasMany = [tasks:Task]
  6.   static mappedBy = [tasks:"owner"]
  7.    
  8.   static constraints = {
  9.     name(blank: false, unique: true)   
  10.   }
  11. }

Task.groovy:

GROOVY:
  1. class Task {
  2.   String subject
  3.   String description
  4.    
  5.   User owner
  6.  
  7.   static belongsTo = User   
  8.   static constraints = {
  9.     subject(blank: false)
  10.     owner(nullable: false) 
  11.   }
  12. }

As you can see I also added some basic constraints to the model. After starting the application the database layout is automatically created by Hibernate using Hibernates' create-drop option (this is configurable).

For those familiar to Hibernate, note the static mapping properties. All of them can take arrays (belongsTo as well) containing the entire mapping.

CRUD
Now, to actually get something on our screen... let's have some scaffolding fun

grails generate-all Task
grails generate-all User

The above generates a CRUD interface (controller + views) for domain class, including a selectbox to select the owner from the list of available users. How cool is that.

And the fun part is: you can start it directly without configuring a database... by default Grails will give you a configured HSQL database!

Now after starting the application we get something like this:

Controller overviewValidation out of the boxSortable listingsNotice the user select box?

I must confess that I had to change a couple of view files to get the select box to display to name of the user instead of the id:

XML:
  1. <g:select optionKey="id" optionValue="name" from="${User.list()}" name='owner.id' value="${task?.owner?.id}"></g:select>

I'll probably dive into the tag part (g namespace, which actually is a JSP taglib) next time, for now just believe that the above generates a select box!

Conclusion
So far so good. Till now I'm really pleased by what I see. Having done some Rails I must say that Grails somehow feels a bit more intuitive for me, but this might be due to the fact that I have seen Rails before. Apart from that, it's all very similar. The fact that you don't have to configure a database to get started is a big plus though! And from what I've seen in the documentation so far, there is a huge load of cool stuff to explore!


1 Response to “Grails - associations”

  1. 1 Wicket Web Beans at log4p

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures

neal ford @ javasummercamp 2008 Moscone Center - JavaOne dorade sjoerd_met_koffer golden_gate_warning_sign IMG_4606.JPG bonen_maler Joshua Bloch at JavaOne2008 IMG_4654.JPG red bar IMG_4597 olijfboom War Room - Diagrams Band Photo trap kikker (frog) javone 2008 closing keynote Stage being build in the nearby park bridge
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.