Mapping Inheritance
By default, GORM uses table-per-hierarchy inheritance, which essentially means the parent and all sub-classes share the same table:
class Content {
String author
}
class BlogEntry extends Content {
URL url
}
class Book extends Content {
String ISBN
}
class PodCast extends Content {
byte[] audioStream
}The above then allows you to perform polymorphic queries:
def content = Content.findAll() // find all blog entries, books and pod casts
content = Content.findAllByAuthor('Joe Bloggs') // find all by authordef podCasts = PodCast.findAll() // find only pod casts
Technical note for those interested: Under the covers, only one table is used in the table-per-hierarchy model. A class column specifies the subclass and any fields in the subclasses are included on the same table. Subclass fields cannot be "required" because the underlying columns need to be nullable for the other subclasses. This doesn't however prevent you from using Validation constraints to ensure that subclass fields are "required".
As an alternative, you can tell GORM to use a joined-subclass model to implement inheritance.
class Content {
static mapping = {
tablePerHierarchy false
} String author
}
Technical note for those interested: This will use 4 tables (Content, BlogEntry, Book, and PodCast) with each subclass table containing only those fields unique to it, e.g., Book would contain an ISBN column, but not an audioStream column.