Skip to content

GORM Relationships in the Grails Console

by Jeff

I’ve just started scratching the surface with Grails. A good starting point seems to be understanding GORM or Grails Object Relational Mapping, which is based on Hibernate3.

In looking at the Grails tutorials online, you might be inclined to start using Grails via scaffolding and generating controllers and views. And this is fine, but another perhaps smaller step alternative is to start with the Grails Console and mess around with the relationships you’re likely to form in the database.

I found that diving in with some of the tutorials, it isn’t always obvious why you’d dynamically scaffold versus performing a static scaffold, or you might not pick up on the power of groovy pages immediately. So I found working with the grails console and GORM to start with eliminated some of the mystery, and provided a solid foundation for further learning. In my previous post I discussed some of the subtle, perhaps unexpected, issues with persisting domain objects in the grails console. Here’s how I added relationships (after the jump).

Let’s start with person and characteristic domain objects:

grails create-domain-class person
grails create-domain-class characteristic

This will create domain class Person, which we will edit to look like:

class Person {
        static hasMany = [characteristics:Characteristic]
	static optionals = ['middleInitial']
        static constraints = {middleInitial(maxLength:1)}
	String firstName
	String lastName
	String middleInitial
}

and the Characteristic class:

class Characteristic {

	static belongsTo = Person
	static optionals = ['displayName']
	String name
	String displayName
	Integer systemLevel
}

Now fire up the grails console, you can persist the relationship as follows:

[/java]
def c = new Characteristic(name: “lazy”, displayName: “exudes laziness”, systemLevel: 1)
def p = new Person(firstName: “Jeff”, lastName: “Smith”, middleInitial: “S”,
characteristics:[c])
p.save()
[/java]

After executing, this should display:

Result: Person : 1

And how about some built-in querying?

Domain object plus .get(id) will retrieve by identifier:

def newGuy = Person.get(1)

if(newGuy) {
println newGuy.firstName
} else {
println "No new guy was found!"
}

You should see “Jeff” as the output.

Dynamic finders are where you really see some interesting things:

def f = Person.findByFirstName('Jeff')
if(f) {
  println f.lastName
  println f.characteristics.each{ println it.name }
} else {
  println "No Person with the first name of Jeff was found!"
}
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

2 Comments

  1. Berkay wrote:

    Did not know about the console. nice!

    Friday, May 30, 2008 at 6:10 am | Permalink
  2. Jeff, I’m a big fan of typing “grails console” as well. The coolness really kicks in when you have composition — say a Person has an Address (not 1:M like your example) — you can make calls like p.address.city and dip right into the nested objects from the CLI.

    One suggestion, though. “static optionals” has been deprecated in favor of static constraints “nullable:true” — http://jira.codehaus.org/browse/GRAILS-472

    Cheers,
    s

    Tuesday, June 3, 2008 at 10:23 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*