1

我有关于多对多映射的问题。

[案子]

  • 帐户拥有社区(所有者)
  • 社区有很多帐户(成员)

当我删除社区实例时,所有者帐户也被删除。

我不希望所有者帐户被删除。

我的映射错了?

【领域类】

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

1 回答 1

0

您必须提供与on delete set null映射等效的休眠

已经有一个关于它的问题。请参阅它以获取更多详细信息如何覆盖 Grails GORM 中关系的级联删除?

于 2013-05-31T11:11:54.397 回答