2013-06-01 34 views
0

我有關於多對多映射的問題。GROM創建不必要的表與多對多映射

[案例]

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

格姆創建四個表:

  • 賬戶
  • 社區
  • COMMUNITY_MEMBERS
  • COMMUNITY_OWNER

GORM創建一個表 「COMMUNITY_OWNER(ACCOUNT_ID,OWNER_ID)」。爲什麼GORM創建這個?

此表未使用(請查看Hibernate日誌)我希望GORM不要創建COMMUNITY_OWNER。我的地圖是錯的?

相關問題:cascade delete with many-to-many mapping in Grails

[域類]

class Account { 

    String name 

    static hasMany = [communities: Community] 
    static belongsTo = [Community] 
} 

class Community { 

    String name 
    Account owner 

    static hasMany = [members: Account] 

    static mapping = { 
    owner cascade: 'none' 
    } 
} 

[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) 

[休眠日誌]

INFO hbm2ddl.SchemaExport - Running hbm2ddl schema export 
DEBUG hbm2ddl.SchemaExport - import file not found: /import.sql 
INFO hbm2ddl.SchemaExport - exporting generated schema to database 
DEBUG hbm2ddl.SchemaExport - drop table account if exists 
DEBUG hbm2ddl.SchemaExport - drop table community if exists 
DEBUG hbm2ddl.SchemaExport - drop table community_members if exists 
DEBUG hbm2ddl.SchemaExport - drop table community_owner if exists 
DEBUG hbm2ddl.SchemaExport - create table account (id bigint generated by default as identity, version bigint not null, name varchar(255) not null, primary key (id)) 
DEBUG hbm2ddl.SchemaExport - create table community (id bigint generated by default as identity, version bigint not null, name varchar(255) not null, owner_id bigint not null, primary key (id)) 
DEBUG hbm2ddl.SchemaExport - create table community_members (community_id bigint not null, account_id bigint not null, primary key (community_id, account_id)) 

<<Why create?>> 
DEBUG hbm2ddl.SchemaExport - create table community_owner (account_id bigint not null, owner_id bigint not null) 

DEBUG hbm2ddl.SchemaExport - alter table community add constraint FKA7C52FE92BBE477B foreign key (owner_id) references account 
DEBUG hbm2ddl.SchemaExport - alter table community_members add constraint FK5A8DA6C3C4A6EE81 foreign key (community_id) references community 
DEBUG hbm2ddl.SchemaExport - alter table community_members add constraint FK5A8DA6C398BAC5C1 foreign key (account_id) references account 
DEBUG hbm2ddl.SchemaExport - alter table community_owner add constraint FK12E232DD98BAC5C1 foreign key (account_id) references account 
DEBUG hbm2ddl.SchemaExport - alter table community_owner add constraint FK12E232DD8BE4BF77 foreign key (owner_id) references community 
INFO hbm2ddl.SchemaExport - schema export complete 

<<community_owner not used>> 
DEBUG hibernate.SQL - insert into account (id, version, name) values (null, ?, ?) 
DEBUG hibernate.SQL - insert into account (id, version, name) values (null, ?, ?) 
DEBUG hibernate.SQL - insert into community (id, version, name, owner_id) values (null, ?, ?, ?) 
DEBUG hibernate.SQL - update account set version=?, name=? where id=? and version=? 
DEBUG hibernate.SQL - update account set version=?, name=? where id=? and version=? 
DEBUG hibernate.SQL - insert into community_members (community_id, account_id) values (?, ?) 
DEBUG hibernate.SQL - update account set version=?, name=? where id=? and version=? 
DEBUG hibernate.SQL - update community set version=?, name=?, owner_id=? where id=? and version=? 
DEBUG hibernate.SQL - delete from community_members where community_id=? and account_id=? 
DEBUG hibernate.SQL - delete from community_members where community_id=? 
DEBUG hibernate.SQL - delete from community where id=? and version=? 
+0

我在休眠日誌中沒有看到'community_owner'。 – dmahapatro

+0

休眠日誌丟失。對不起,請再看一遍。 – kanoyan

回答

1

看你的貼圖,我相信你的情況稍微偏離(以及映射)。你的意思是說......

帳戶有許多社區(如「社區」)
社區有許多帳戶(如「成員」)

這將是一個真正的多對-許多。同樣從您的映射中,我假設您希望Account成爲多對多的擁有者。如果是這種情況,那麼你可以做如下的事情。

class Account { 
.. 
static hasMany = [communities: Community] 
static mappedBy = [communities: 'owner'] //<-- will not work without this mapping 
} 

class Community { 
.. 
static belongsTo = [owner: Account] //<-- assumes Account is owner 
static hasMany = [members: Account] 
} 

注意:belongsTo在社區中承擔帳戶將是「所有者」。 mappedBy是必需的,否則Grails會炸燬(這裏是discussion about it)。

你最終會得到帳戶和社區表以及第三個映射表 - 沒有第四個表。

  • 映射表將處理社區「有許多成員」關係
  • 帳戶「有很多社區」的關係是通過在社區表背到帳戶外鍵處理。
+0

你的猜測是正確的。我已經解決了這個問題。謝謝你的詳細解釋!感謝你,我能夠理解。 – kanoyan