I have a one-to-many relationship:
class Author {
String name
static hasMany = [books:Book]
static constraints = {
}
}
class Book {
String name
static belongsTo = [author:Author]
static constraints = {
}
}
I want to be able to count the number of books belonging to the author in the author’s class, so the resulting generated MySQL table will look like this:
id | version | name | bookcount
-- | ------- | ---- | ---------
1 | 0 | foo | 15
2 | 0 | bar | 3
3 | 0 | baz | 7
... where bookcount is a specific field in the author's class:
class Author {
String name
int bookcount = ??
static constraints = {
}
}
EDIT 1: The account must be stored in the database.
source
share