删除m-to-m也是尝试级联删除一个1-2-1

发布于 2024-09-26 22:40:44 字数 738 浏览 6 评论 0原文

我有以下域

class Committee {
   String name
   BoardCommitteeType boardCommitteeType
   Date dateCreated
   Date lastUpdated
   User createdBy
   User modifiedBy

   static belongsTo = [
      board: Board,
   ]

   static hasMany = [          
      members: User
   ]
}

class User {

    static hasMany = [            
        committees: Committee,     
    ]

    static belongsTo = [
        Board, Committee
    ]
}

问题是,当我尝试执行 board.removeFromCommittees(committee) 时,我收到以下异常:

已删除的对象将通过级联重新保存(从关联中删除已删除的对象):[com.wbr .highbar.User#1];

我明白这意味着什么。我不明白的是为什么我会得到它。另一个有趣的地方是,如果我将委员会实例中的 creatdBy 和 ModifiedBy 设置为空,则删除工作正常。这就是为什么我认为 GORM 正在尝试级联 1-2-1。我的理论是,这与用户属于委员会这一事实有关。但我不知道如何解决这个问题。

I have the following Domains

class Committee {
   String name
   BoardCommitteeType boardCommitteeType
   Date dateCreated
   Date lastUpdated
   User createdBy
   User modifiedBy

   static belongsTo = [
      board: Board,
   ]

   static hasMany = [          
      members: User
   ]
}

class User {

    static hasMany = [            
        committees: Committee,     
    ]

    static belongsTo = [
        Board, Committee
    ]
}

The problem is that when I attempt to do a board.removeFromCommittees(committee) I'm getting the following exception:

deleted object would be re-saved by cascade (remove deleted object from associations): [com.wbr.highbar.User#1];

I understand what that means. What I don't understand is why I am getting it. The other interesting bit is that if I make creatdBy and modifiedBy in the Committee instance null, the delete works just fine. That's why I am thinking that GORM is trying cascade the one-2-one. My theory is that is has something to do with the fact User belongsTo a Committee. But I don't know how to fix the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一腔孤↑勇 2024-10-03 22:40:45

级联删除受域类之间的 belongsTo 关系影响。

由于委员会属于董事会,当董事会被删除时,删除会级联到委员会。由于用户属于委员会,当委员会被删除时,删除会级联到用户。

解决您的问题的方法是删除 User ownsTo Commission 关系。

关于整个域模型的注释:

您有很多多对多关系。他们不一定是错的,但他们可能使事情过于复杂化。你可能可以只使用:

class Committee {
    static hasMany = [boards: Board, users: User]
}

class Board {
    static hasMany = [users: User]
    static belongsTo = Committee // this is the only belongsTo you need, since if a
                                 // committee is dissolved, presumably the board
                                 // will be dissolved as well (unless you have
                                 // cross-committee boards)
}

class User {
    // doesn't define any relationships,
    // let the Committee/Board domains handle everything

    // also, this is presuming that users are at a higher level than committees, i.e.
    // a user could belong to multiple committees
}

The cascading delete is effected by the belongsTo relationships between your domain classes.

Since Committee belongsTo Board, when a Board gets deleted, the delete cascades to the Committee. Since User belongsTo Committee, when a Committee gets deleted, the delete cascades to the User.

The solution to your problem is to remove the User belongsTo Committee relationship.

Notes on your domain model as a whole:

You have a lot of many-to-many relationships. They're not necessarily wrong, but they might be overcomplicating things. You could probably get away with just using:

class Committee {
    static hasMany = [boards: Board, users: User]
}

class Board {
    static hasMany = [users: User]
    static belongsTo = Committee // this is the only belongsTo you need, since if a
                                 // committee is dissolved, presumably the board
                                 // will be dissolved as well (unless you have
                                 // cross-committee boards)
}

class User {
    // doesn't define any relationships,
    // let the Committee/Board domains handle everything

    // also, this is presuming that users are at a higher level than committees, i.e.
    // a user could belong to multiple committees
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文