Grails hasMany 和 hasOne 在反面

Grails hasMany with hasOne on the reverse side

我有两个表之间的简单关系如下:

class Book {
  String title
  static hasMany = [authors: Author]
}

class Author {
  String Name
}

我有一个要求,虽然一本书可以有很多作者,但一个作者只能有一本书。作者可以独立于书籍而存在。

在什么条件下我可以确保一位作者可以拥有一本书?

class Book {
  String title
  static hasMany = [authors: Author]
}

class Author {
  String Name
  Book book  // this will be the belongTo relationship that you need

  static constraints = {
      book nullable:true
  }
}