Dave Cherry has written a GORM guide.
GORM is the data abstraction layer that is used to map domain objects to database tables in Grails. Although GORM can be used outside of Grails, we do not discuss that here, we assume that you are using Grails. In fact we assume you are using V1.0 of Grails.
Grails gets you started quickly, when you create a new project, setting up the database for development should not be required, this is because Grails creates a default datasource that uses an in-memory database.
Unlike Ruby on Rails, GORM starts with the definition of a POGO (groovy object). This object is then used as the template to build the table on the database. Although this can be changed, it is by convention the default way. This article assumes that you are familiar with Grails, and have already created a test project that can be used for this article.
Though GORM depends on some bootstrap code done by Grails core, you can pretty much use it outside of Grails, as Jeremie Wilden shows at Using GORM in a Desktop Application. Dave takes care to point out a crucial difference from RoR: domain models are the primary building blocks in Grails, not the database. These means you are free to model your domain without being restricted by a particular database schema. Usually what happens with a brand new Grails application the database will follow the domain. Then again, because Grails follows the Convention over Configuration paradigm but doesn’t dismiss configuration, you’ll be able to map a legacy database schema to a freshly designed domain. This means you do not have to throw away your current web application in order to take advantage of Grails.
Dave outlines the basics of a domain model, how to map domain constraints which will be reused on views and database schema generation. He also demonstrates two of the three supported relationship mappings
- one to many
- many to one
Leaving the many-to-many relationship without example because of the following
If you’ve used Hibernate before, you may be used to mapping many-to-many tables in an invisible way, at the moment GORM does not provide this. Instead the join table must be included in your domain model; then you use the many-to-one and one-to-many support we’ve already seen.
For example if added a ‘Keywords’ object that linked to many Books, and each Books could have many Keywords, we would create a new domain object something like
KeywordBookLinkthat had a one-to-one link with both Keyword and Book. In addition it would be wise to make either Book or Keyword the owner of the link object.
Though it looks like a limitation of Grails/GORM, Graeme Rocher and Dierk König have said before that an explicit joint table is in fact a good idea. Once you reflect on why you need it you may discover additional properties that can be applied to the relationship itself that do not belong to either side per se.
Other topics discussed in the article are
- basic CRUD operations
- dynamic listings
- dynamic finders
the article is not a full description of what you can accomplish with GORM, but it is a very good read for those wanting to learn one of the most powerful tools in the Grails toolbox.
Tags: Dave Cherry, Grails


About