GROM使用多对多映射创建不必要的表 [英] GROM create needless table with many-to-many mapping

查看:93
本文介绍了GROM使用多对多映射创建不必要的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多对多映射的问题。

[案例]


  • 帐户拥有社区(所有者)

  • 社区有许多帐户(成员)



GORM创建了四个表格:




  • ACCOUNT

  • 社区

  • COMMUNITY_MEMBERS

  • COMMUNITY_OWNER



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



这张表没有被使用(请看看Hibernate日志)我希望GORM不要创建COMMUNITY_OWNER。我的地图是错误的?



相关问题:



[域类]

  class Account {

字符串名称

static hasMany = [社区:社区]
静态belongsTo = [社区]
}

类社区{

字符串名称
帐户所有者

static hasMany = [会员:账户]

静态映射= {
所有者级联:'无'
}
}
$ b

[TestCode]

  def admin = new Account(name: 'admin')。save(flush:true)
def user = new Account(name:'user')。save(flush:true)
$ b def c = new Community(name: 'TestCommunity')

c.owner = admin

c.addToMembers(admin)
c.addToMembers(user)
c.save(flush :TRU e)

c.removeFromMembers(用户)
c.save(flush:true)

c.delete(flush:true)

[Hibernate日志]

 信息hbm2ddl.SchemaExport  - 运行模式就是hbm2ddl出口
DEBUG hbm2ddl.SchemaExport - 导入文件未找到:/import.sql
INFO hbm2ddl.SchemaExport - 导出生成的模式数据库
DEBUG hbm2ddl.SchemaExport - 删除表帐户(如果存在)
DEBUG hbm2ddl.SchemaExport - 删除表社区(如果存在)
DEBUG hbm2ddl.SchemaExport - 删除表community_members(如果存在)
DEBUG hbm2ddl.SchemaExport - 删除表community_owner if exists
DEBUG hbm2ddl.SchemaExport - 创建表帐户(id bigint默认生成为标识,版本bigint不为null,name varchar(255)非空,主键(id))
DEBUG hbm2ddl.SchemaExport - 创建表社区(默认生成的id为bigint作为标识,版本bigint不为null,name varchar(255)不为null,owner_id bigint不主键(community_id,account_id))

DEBUG hbm2ddl.SchemaExport - 创建表community_owner(account_id bigint不为null,owner_id bigint不为null)

DEBUG hbm2ddl.SchemaExport - alter table社区添加约束FKA7C52FE92BBE477B外键(owner_id)引用帐户
DEBUG hbm2ddl.SchemaExport - alter table community_members添加约束FK5A8DA6C3C4A6EE81外键(community_id)引用社区
DEBUG hbm2ddl.SchemaExport - alter table community_members添加约束FK5A8DA6C398BAC5C1外键(account_id)引用帐户
DEBUG hbm2ddl。 SchemaExport工具 - 改变表community_owner添加约束FK12E232DD98BAC5C1外键(ACCOUNT_ID)引用占
DEBUG hbm2ddl.SchemaExport - 改变表community_owner添加约束FK12E232DD8BE4BF77外键(owner_id)引用社区
INFO hbm2ddl.SchemaExport - 架构出口完成

<<<< 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 - 插入社区(id,版本,名称,owner_id)values(null,?,?,?)
DEBUG hibernate.SQL - 更新帐户集版本=? ,name =?其中id =?和版本=?
DEBUG hibernate.SQL - 更新账户设置版本= ?,名称=?其中id =?和版本=?
DEBUG hibernate.SQL - 插入到community_members(community_id,account_id)值(?,?)
DEBUG hibernate.SQL - 更新帐户集版本= ?,名称=?其中id =?和版本=?
DEBUG hibernate.SQL - 更新社区设置版本=?,名称=?,owner_id =?其中id =?和版本=?
DEBUG hibernate.SQL - 从community_members删除community_id =?和account_id =?
DEBUG hibernate.SQL - 从community_members删除community_id =?
DEBUG hibernate.SQL - 从社区中删除id =?和版本=?


解决方案

关闭(以及映射)。你的意思是说......


帐户有许多社区(如'社区')

社区有许多帐户(作为'会员')


这将是一个真正的多对多。同样从您的映射中,我假设您希望Account成为多对多的拥有者。如果是这种情况,那么你可以做类似下面的事情。

  class Account {
..
静态hasMany = [社区:社区]
静态mappedBy = [社区:'所有者'] //< - 在没有此映射的情况下无法运行
}

class社区{
..
static belongsTo = [owner:Account] //< - 假设账户是所有者
static hasMany = [会员:账户]
}

注意:社区假设帐户中的 belongsTo 所有者。 mappedBy是需要的,否则Grails会炸毁(这是一个关于它的讨论)。



您最终会得到帐户和社区表格以及第三个映射表格 - 不是第四个表格。


  • 映射表将处理社区有许多成员的关系

  • 账户has许多社区关系由社区表上的外键处理回账户。


I have problem about many-to-many mapping.

[Case]

  • Account owns Community(owner)
  • Community has many Account(members)

GORM create four tables:

  • ACCOUNT
  • COMMUNITY
  • COMMUNITY_MEMBERS
  • COMMUNITY_OWNER

GORM creates a table "COMMUNITY_OWNER (ACCOUNT_ID, OWNER_ID)". Why GORM creating this?

This table is not used.(Please look at Hibernate log) I want GORM to not create COMMUNITY_OWNER. My mapping is wrong?

Related Question: cascade delete with many-to-many mapping in Grails

[Domain Class]

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)

[Hibernate Log]

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

解决方案

Looking at your mapping, I believe your case is slightly off (as well as the mapping). Did you mean to say ...

Account has many Community (as 'communities')
Community has many Account (as 'members')

this would be a true many-to-many. Also from your mapping, I'm assuming you'd like Account to be the owning side of the many-to-many. If this the case, then you can do something like the following.

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

NOTE: the belongsTo in Community assumes Account will be the "owner". The mappedBy is needed otherwise Grails will blow up (here's a discussion about it).

You'll end up with the Account and Community tables and a third mapping table - no fourth table.

  • the mapping table will handle the Community "has many members" relationship
  • the Account "has many communities" relationship is handled by a foreign key on the Community table back to Account.

这篇关于GROM使用多对多映射创建不必要的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆