【发布时间】:2014-02-16 02:44:27
【问题描述】:
我在 GORM 中创建多对多双向关系时遇到问题,我找到的解决方案并不是我真正想做的。
我目前建立的关系允许一个作者拥有多本书,但反之则不行(所有权在作者一方)。这是我目前拥有的代码。
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
}
}
我希望这种关系是这样的,当我编辑一本书时,我可以选择多个作者,除了在我编辑一个作者时有同一作者的多本书。
【问题讨论】:
标签: grails many-to-many grails-orm bidirectional