在Grails中使用多对多映射级联删除 [英] cascade delete with many-to-many mapping in Grails
问题描述
我有多对多映射的问题。
[案例]
当我删除社区的实例时,所有者帐户也被删除。
我不希望所有者帐户被移除。
我的映射是错误的?
[域类]
class Account {
字符串名称
static hasMany = [communities:Community]
static belongsTo = [Community]
}
class社区{
字符串名称
帐户所有者
static hasMany = [会员:帐户]
}
[TestCode]
def admin = new Account(name:'admin')。save(flush:true)
def user = new Account(name:'user')。save(flush:true)
def c = new Community(name:'TestCommunity')
c.owner = admin
c.add ToMembers(admin)
c.addToMembers(user)
c.save(flush:true)
c.removeFromMembers(user)
c.save(flush: true)
c.delete(flush:true)
$ p $ Hibernate:insert into account(id,version,name)values(null,?,?)
Hibernate :插入到社区(id,版本,名称,owner_id)值(空,?,?,?)
休眠:更新帐户设置版本= ?,名称=?其中id =?和版本=?
Hibernate:插入到community_members(community_id,account_id)值(?,?)
Hibernate:更新帐户集版本=?,name =?其中id =?和版本=?
Hibernate:更新社区设置版本=?,名称=?,owner_id =?其中id =?和版本=?
Hibernate:从community_members删除community_id =?和account_id =?
Hibernate:从community_members删除community_id =?
Hibernate:从社区中删除id =?和版本=?
Hibernate:从账户中删除id =?和版本=? < ==不是预期的!
您必须提供相当于 on delete set
mapping
已经有一个关于它的问题。请参阅它进一步的细节如何覆盖Grails GORM中关系的级联删除?
I have problem about many-to-many mapping.
[Case]
- Account owns Community(owner)
- Community has many Account(members)
When I delete instance of Community, owner account is also delete.
I don't expect owner account to be removed.
My mapping is wrong?
[Domain Class]
class Account {
String name
static hasMany = [communities: Community]
static belongsTo = [Community]
}
class Community {
String name
Account owner
static hasMany = [members: Account]
}
[TestCode]
def admin = new Account(name: 'admin').save(flush:true)
def user = new Account(name: 'user').save(flush:true)
def c = new Community(name: 'TestCommunity')
c.owner = admin
c.addToMembers(admin)
c.addToMembers(user)
c.save(flush:true)
c.removeFromMembers(user)
c.save(flush:true)
c.delete(flush:true)
[Hibernate Log]
Hibernate: insert into account (id, version, name) values (null, ?, ?)
Hibernate: insert into community (id, version, name, owner_id) values (null, ?, ?, ?)
Hibernate: update account set version=?, name=? where id=? and version=?
Hibernate: insert into community_members (community_id, account_id) values (?, ?)
Hibernate: update account set version=?, name=? where id=? and version=?
Hibernate: update community set version=?, name=?, owner_id=? where id=? and version=?
Hibernate: delete from community_members where community_id=? and account_id=?
Hibernate: delete from community_members where community_id=?
Hibernate: delete from community where id=? and version=?
Hibernate: delete from account where id=? and version=? <== not expected !!
You have to provide hibernate equivalent to on delete set null
mapping
There is already a question about it. Please refer to it for further details How do I override the cascade delete for a relation in Grails GORM?
这篇关于在Grails中使用多对多映射级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!