The embedded component feature of Grails is not documented very well, in my opinion. Therefore, I thought I would create a blog entry so others don't have to spend the time trying to experiment with it to get it to work.
I recently spent some time working on a home project that I have implemented in Grails 1.1. Grails has a feature for embedding domain components into other domain classes. An embedded component participates in the domain class mapping to a database table; there is no join to an child embedded component table. In my domain object model, a TimeRecord is a domain component suitable for embedding. It doesn't live on its own, but is meant to be embedded as a reusable component throughout my domain object model. Grails has first-class support for embedded components, though it took me a bit of time to figure out how to get it to work. First the definition of the TimeRecord class:
class TimeRecord {
TimeRecordUnits units
BigDecimal value
static constraints = {
units(nullable: false)
value(nullable: false)
}
}
Nothing earth-shattering here. The reference to TimeRecordUnits is a Groovy enum. The TimeRecord class is not meant to be mapped to its own table in the database; it will become part of any domain object's table mapping whenever it is embedded in that domain object class. Thus, the TimeRecord class definition needs to reside inside some other domain object's Groovy file. Strange behavior, even for convention over configuration, but it does work.
Now embed the TimeRecord in another domain object class and that domain object's table mapping will also have TimeRecord properties mapped to it. Here is my Story class that has a TimeRecord contained in it:
class Story {
TimeRecord estimate
static embedded = ['estimate']
}
I've removed other Story properties to focus on the embedded component mapping. Grails has a static property named embedded that specifies the component object property that should participate as an embedded association. That's it. I put the TimeRecord definition in the Story.groovy file, directly after the Story definition. Everything maps correctly to the database and I don't get an extraneous time_record table being generated by GORM. Pretty cool.
I must say that domain object modeling in Grails is much, MUCH faster than it is in Java with Hibernate. I'm at least an order of magnitude faster with GORM in Grails than I was in Java and Hibernate. I'm hooked on Grails convention over configuration theme.
Powered by Zoundry Raven