我有关于多对多映射的问题。
[案子]
- 帐户拥有社区(所有者)
- 社区有很多帐户(成员)
当我删除社区实例时,所有者帐户也被删除。
我不希望所有者帐户被删除。
我的映射错了?
【领域类】
class Account {
String name
static hasMany = [communities: Community]
static belongsTo = [Community]
}
class Community {
String name
Account owner
static hasMany = [members: Account]
}
[测试代码]
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: 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 !!