Grails 一次添加多个属于父项的子项

Grails 一次添加多个属于父项的子项,grails,gorm,Grails,Gorm,如果我有两个域,如下所示 class Author { static hasMany = [books: Book] String name } class Book { static belongsTo = [author: Author] String color } 如何同时为一位作者添加多本书 如下图所示: def book1 = new Book(color: "white") def book2 = new Book(color: "black") def boo

如果我有两个域,如下所示

class Author {
  static hasMany = [books: Book]
  String name
}

class Book {
  static belongsTo = [author: Author]
  String color
}
如何同时为一位作者添加多本书

如下图所示:

def book1 = new Book(color: "white")
def book2 = new Book(color: "black")
def books = []
books << book1
books << book2

def author = new Author(name: "John Doe").addToBooks(books).save()
def book1=新书(颜色:“白色”)
def book2=新书(颜色:“黑色”)
def books=[]

books
addToBooks
获取一个
Book
实例或一个可用于创建
Book
实例的映射。这是相对紧凑的:

def book1 = new Book(color: "white")
def book2 = new Book(color: "black")
def books = []
books << book1
books << book2

def author = new Author(name: "John Doe")
books.each { author.addToBooks(it) }
author.save()
def book1=新书(颜色:“白色”)
def book2=新书(颜色:“黑色”)
def books=[]

books
addToBooks
获取一个
Book
实例或一个可用于创建
Book
实例的映射。这是相对紧凑的:

def book1 = new Book(color: "white")
def book2 = new Book(color: "black")
def books = []
books << book1
books << book2

def author = new Author(name: "John Doe")
books.each { author.addToBooks(it) }
author.save()
def book1=新书(颜色:“白色”)
def book2=新书(颜色:“黑色”)
def books=[]

books
addToBooks
获取一个
Book
实例或一个可用于创建
Book
实例的映射。这是相对紧凑的:

def book1 = new Book(color: "white")
def book2 = new Book(color: "black")
def books = []
books << book1
books << book2

def author = new Author(name: "John Doe")
books.each { author.addToBooks(it) }
author.save()
def book1=新书(颜色:“白色”)
def book2=新书(颜色:“黑色”)
def books=[]

books
addToBooks
获取一个
Book
实例或一个可用于创建
Book
实例的映射。这是相对紧凑的:

def book1 = new Book(color: "white")
def book2 = new Book(color: "black")
def books = []
books << book1
books << book2

def author = new Author(name: "John Doe")
books.each { author.addToBooks(it) }
author.save()
def book1=新书(颜色:“白色”)
def book2=新书(颜色:“黑色”)
def books=[]

书籍如果你的目标是减少代码行,那么你也可以通过删除本地列表并直接使用
[book1,book2].each{author.addtobook(it)}
来优化Burt的代码。如果你的目标是减少代码行,那么你也可以通过删除本地列表并使用
[book1,book2].each{author.addtobook(it)}来优化Burt的代码(it)}
直接。如果你的目标是减少代码行数,那么你也可以通过删除本地列表并使用
[book1,book2]来优化Burt的代码。每个{author.addToBooks(it)}
直接。如果你的目标是减少代码行数,那么你也可以通过删除本地列表并使用
[book1,book2]来优化Burt的代码。每个{author.addToBooks(it)}
直接。