0

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

[案子]

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

GORM 创建四个表:

  • 帐户
  • 社区
  • COMMUNITY_MEMBERS
  • COMMUNITY_OWNER

GORM 创建一个表“COMMUNITY_OWNER (ACCOUNT_ID, OWNER_ID)”。为什么 GORM 创建这个?

该表没有被使用。(请查看 Hibernate 日志)我希望 GORM 不创建 COMMUNITY_OWNER。我的映射错了?

相关问题:在 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'
  }
}

[测试代码]

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=?
4

1 回答 1

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]
}

注意:belongsToin Community 假设 Account 将是“所有者”。需要 mappedBy 否则 Grails 会崩溃(这里有关于它的讨论)。

您最终会得到 Account 和 Community 表以及第三个映射表 - 没有第四个表。

  • 映射表将处理社区“有很多成员”的关系
  • 帐户“有许多社区”关系由社区表上的外键处理回帐户。
于 2013-06-01T08:52:38.743 回答