在Grails中,如果存在多对多关系,例如book has many authors
让我们说book1 has author1,autho2,author3,author4值。现在发送一个PUT请求,说book1只有author1,author3,然后从表中删除其他两个值。

现在对于具有一对多关系的相同场景,如果仅使用author1,author3完成PUT请求,则现在说book1 has author1,autho2,author3,author4

是否应该删除其他两个i.e, author2 and author4值?我希望这种行为是..

以下是我的书籍和作者模型

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

     static mapping = {
    books cascade: 'all-delete-orphan'
}
}

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

编辑:

当我实施所有删除的孤儿时,出现了Followinh错误
拥有实体实例不再引用具有“cascade =“all-delete-orphan”的集合

最佳答案

这取决于级联选项集。如果您在belongsTo表上使用Author,则grails默认使用cascade all。这意味着,如果其他Author不使用Book对象,则将其删除。您可以使用cascade选项自定义此行为(all-delete-orphan应该是您感兴趣的值)。

10-06 16:13