Grails Object Relational Mapping
“Stone me! that’s a boring blog title, can’t we have more stuff about Ukuleles or pictures of pints of beer” I hear you cry.
Well no.
Grails has a nice way of mapping domain objects together, see these objects for a pretend GTD app:
class Project {
String description
String type
Integer priority
Date dueDatestatic hasMany = [ nextActions:NextAction]
static constraints = {
description(nullable:false,length:1..50)
type(inList:[”Home”,”Work”])
type(nullable:false)
}}
class NextAction {String description
String context
String notes
Integer prioritystatic constraints = {
description(blank:false,length:5..30)
context(nullable:false,blank:false,inList:[”email”,”errand”])
}static belongsTo = Project
}
Hopefully you can easily see the magic here. The hasMany and belongsTo settings join these two objects together in one to many relationship. Better still when you run the Grails app it auto generates the database schema for these objects and an intermediate table to join em - possibly a bit too normalized for me but still.
Even better, if I choose to add a new property to one of the classes the application relaunches after I make the save and the new column is added to the database schema.
Oooo its like magic.
Sure beats the make change, add some xml, recompile, look out of window and restart context hell of normal Java development. Sigh.
Leave a Reply