cascade delete with many-to-many mapping in Grails

扶醉桌前 提交于 2019-12-08 11:44:33

问题


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 !!

回答1:


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?



来源:https://stackoverflow.com/questions/16855838/cascade-delete-with-many-to-many-mapping-in-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!