我离开 Grails 有一段时间了,所以我尝试创建具有一些基本域关系(一对多、多对多)的演示 rest-api 应用程序并遇到了一些奇怪的问题。简而言之,我有 4 个域类,如下所示:
class Publisher {
String name
static hasMany = [books:Book]
}
class Author {
String name
static hasMany = [books: Book]
}
class Category {
String name
static belongsTo = Book
static hasMany = [books:Book]
}
class Book {
String title
Publisher publisher
static hasMany = [categories: Category]
}
我正在尝试在 bootrap.groovy 中插入一些演示数据(使用多种不同的方法),但是应该进入联合表的数据不会持久化(空)。例如,即使使用 cascade-create,'edge' 记录也会被持久化(例如从 boook 创建的类别),但是它们之间的连接表中没有数据):
Bootstrap.groovy 使用不同的方法插入记录:
// Stand-alone category
def science = new Category(name: 'science').save(failOnError: true)
// publishers
def manning = new Publisher(name: 'manning').save(failOnError: true)
def amazon = new Publisher(name: 'amazon').save(failOnError: true)
// Create single author
def johnDoe = new Author(name: 'John Doe').save(failOnError: true)
// Create-cascade from Author-Book-Category with explicid 'new' and 'save' Book
def jackDanields = new Author(name: 'Jack Daniels')
.addToBooks(new Book(title: 'Hate book', publisher: manning).addToCategories(name: 'love').save())
.addToBooks(new Book(title: 'Fiction book', publisher: manning).addToCategories(name: 'fiction').save())
.save(failOnError: true)
// Create-cascade from Author-Book without explicit save of book
def zoran = new Author(name: 'Zoran')
.addToBooks(title: 'First book', publisher: manning)
.addToBooks(title: 'Second book', publisher: manning)
.addToBooks(title: 'Third book', publisher: manning)
.save(failOnError: true)
我尝试了 H2 和 MariaDb,结果是一样的。github 上提供了完整的项目:https ://github.com/zbubric/grails4-rest-sample
那么,我错过了什么或者它是一些已知的问题/功能?