I'm having a problem with creating a many-to-many bidirectional relationship in GORM, and the solutions I'm finding aren't really what I would like to do.
The relationship I currently have set up allows an author to have multiple books, but not the other way around (ownership is on the author side). This is the code I currently have.
class Author {
String name
static hasMany = [books:Book]
static constraints = {
name(nullable:false)
}
String toString() {
name
}
}
class Book {
String name
String type
Integer year
Author authors
static belongsTo = [authors:Author]
static hasMany = [authors:Author]
static constraints = {
name(nullable:false)
type(nullable:false)
year(nullable:true)
authors(nullable:false)
}
String toString() {
name
}
}
I would like the relationship to be such that when I edit a book, I can select multiple authors, in addition to having multiple books by the same author when I edit an author.
Grails supports many to many realtionships, and your code must work properly... but there is one little problem... scaffolding doesnt support it, so you have to write your own code to handle the relationship as you want!