Grails:Author.withTransaction{} 和 Book.withTransaction{} 之间的区别,如果 Author 和 Book 都应保存在该事务中

Grails: Difference between Author.withTransaction{} and Book.withTransaction{} if both Author and Book should be saved in that transaction

Grails 版本 3.2.9

Grails documentation 对于 withTransaction 并没有说明使用 Author.withTransactionBook.withTransaction 之间的区别是什么,以防万一我想同时保存 AuthorBook 该事务中的域实例如下所示:

Author author = Author.get(1)
Book book = Book.findByAuthor(author)

Author.withTransaction { // what if we use Book here instead of Author
    author.age = 39
    book.price = 45

    author.save(failOnError: true)
    book.save(failOnError: true)
}

未找到解释差异的任何其他文档。

没有区别,他们做同样的事情。每个域 class 都用 withTransaction 装饰,这是相同的跨国代码。您可以使用 Foo.withTransaction,甚至不做任何与 Foo 相关的事情,这没关系。