1

假设您有两个域类:Authors 和 Books。作者可以有很多书,但一本书只能有一个作者。

class Author {
  static hasMany = [Book]
}

class Book {
  static belongsTo = Author
}

你怎么查询是书的作者?

这似乎不起作用:

def book = Book.get(1)
book.author 
4

1 回答 1

4

将您的代码更改为:

class Author {
  static hasMany = [book:Book]
}

class Book {
  static belongsTo = [author:Author]
}

那么这应该工作:

def book = Book.get(1)
book.author
于 2010-12-27T04:50:38.563 回答