假设您有两个域类:作者和书籍。作者可以有很多书,但一本书只能有一个作者。
class Author {
static hasMany = [Book]
}
class Book {
static belongsTo = Author
}
你如何查询书的作者?
这似乎不起作用:
def book = Book.get(1)
book.author
答案 0 :(得分:4)
将您的代码更改为:
class Author {
static hasMany = [book:Book]
}
class Book {
static belongsTo = [author:Author]
}
那么这应该有效:
def book = Book.get(1)
book.author