I am trying to implement two different types of relationships between two domain classes in Grails.
Consider the following; I have two domain classes, an Author and Book class with an Author having many Books.
class Author{
String name
}
class Book{
String title
static belongsTo = [author:Author]
}
The above depicts a very basic one to many relationship between an Author and a Book. But I also want an Author to have a concept of a list of favourite books. This would ideally be represented as a separate one to many relationship describing the same Book object as a list and persisted as such.
class Author{
String name
static hasMany = [favouriteBooks: Book]
static mapping = {
favouriteBooks joinTable: [name: 'favourite_books',
key: 'author_id']
}
}
class Book{
String title
static belongsTo = [client:Client]
}
I have tried to describe this as above (among many other methods) and ultimately the database table (favourite_books) is not created. I do not get any errors. This is the only way I can think of doing this without using any extra objects which I would like to avoid to keep the model simple. I feel I'm on the right track but maybe missing some vital piece of the puzzle.
Any help would be much appreciated.
clean
– ikumenhasMany
property on theAuthor
side and in the second example there's no reference to thefavouriteBooks
relatiionship on theBook
side. – Dónal