(Quick Reference)



Supports embedding domain components into domain classes - otherwise known as composition.


Given a Person domain class and Country class like so:

class Person {
    String name

Country bornInCountry Country livesInCountry

static embedded = ['bornInCountry', 'livesInCountry'] }

// If you don't want an associated table created for this class, either // define it in the same file as Person or put Country.groovy under the // src/groovy directory. class Country { String iso3 String name }

Grails will generate a person database table that looks like:



An embedded component does not store its data in a separate table as a normal domain class relationship does. Instead, the data is embedded in the owner's table. So in the above example, the Country fields appear in the person table. This means that queries are faster because there is no join, but you may end up with duplicate data across tables.

The embedded component class is typically declared in the same file as the owning class or in its own file under src/groovy. You can put the component class under grails-app/domain, but if you do so Grails will automatically create a dedicated table for it. Putting the class under src/groovy is usually the best thing to do because you can then share the component across multiple domain classes.

Querying on embedded properties is no different from querying on normal relationships, so you can for example still do:


where brazil and france are instances of Country.