2013-05-31 194 views
0

我有關於多對多映射的問題。級聯刪除與Grails中的多對多映射關係

[案例]

  • 帳戶擁有共同體(所有者)
  • 社區有許多客戶(會員)

當我刪除社區的情況下,所有者帳戶也被刪除。

我不希望所有者帳戶被刪除。

我的映射是錯誤的?

[域類]

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

回答

0

你必須提供休眠相當於on delete set null映射

已經有一個關於它的問題。請參閱它的進一步細節How do I override the cascade delete for a relation in Grails GORM?

+0

謝謝你的答案。它按預期工作。但是,我還有一個問題。 GORM創建一個表「COMMUNITY_OWNER(ACCOUNT_ID,OWNER_ID)」。爲什麼GORM創建這個? – kanoyan

+1

你能把它變成另一個問題嗎?如果我的答案足夠了,請考慮接受它。 – WeMakeSoftware

+0

對不起。請原諒粗魯。我將創建另一種形式。謝謝你的幫助。 – kanoyan