删除m-to-m也是尝试级联删除一个1-2-1
我有以下域
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
级联删除受域类之间的
belongsTo
关系影响。由于
委员会属于董事会
,当董事会被删除时,删除会级联到委员会。由于用户属于委员会
,当委员会被删除时,删除会级联到用户。解决您的问题的方法是删除
User ownsTo Commission
关系。关于整个域模型的注释:
您有很多多对多关系。他们不一定是错的,但他们可能使事情过于复杂化。你可能可以只使用:
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. SinceUser 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: