简体   繁体   English

Grails / GORM:域和关联如何映射到MySql数据库

[英]Grails/GORM: How do Domains and Associations map to a MySql Database

I'm working with GORM via Grails 3 and was wondering how domains and their associations look like when output to a MySql database. 我正在通过Grails 3使用GORM,并且想知道将域及其关联输出到MySql数据库时的外观。

I'm really looking for sort of a "cheat sheet" guide. 我真的在寻找某种“备忘单”指南。 Something that, for example, states: 举例说明:

String                 --> this column data type 

tablePerHierarchy true --> these tables 

hasMany                --> this table/join table 

hasMany & belongsTo    --> ...

I thought this would be a quick Google job, but I haven't turned up anything so far. 我以为这是Google的快速工作,但到目前为止我还没有发现任何东西。 I also understand that this is based on Hibernate so I've tried searching there as well. 我也了解这是基于Hibernate的,因此我也尝试在其中进行搜索。 I may just render out these tables and note this down myself here, but before I do that does anyone in the community know if such a resource already exists? 我可能只是渲染了这些表并在这里自己记下来,但是在此之前,社区中是否有人知道这种资源是否已经存在?

Best of all, just look in your database, it will be easier to see live, use DBMS for this (for example HeidiSQL , Workbench etc) 最重要的是,只需查看您的数据库,就可以更轻松地进行实时查看,为此使用DBMS (例如HeidiSQLWorkbench等)

This is some examples of default data types -> 这是默认数据类型的一些示例->

1.) Simple data types : 1.) 简单数据类型

String     -> VARCHAR(255)
Integer    -> BIGINT
BigDecimal -> DOUBLE
Boolean    -> BIT
Date       -> DATETIME

2.) Storage of domains relationships *(To store relations beetwen domains in database gorm adds id (BIGINT) and foreign keys ) 2.) 域关系的存储 *(要在数据库gorm中存储关系beetwen域,请添加id (BIGINT)和外键

a.) hasMany : a。) hasMany:

  class People { 
      ...
     hasMany = [men : Man]
  }

As there many men in people that is why we store reference key in Man domain. 由于人们中有很多男人,因此我们将参考密钥存储在Man域中。 ( This mean people table in db will be equivalent to table without hasMany relation ) 这意味着db中的people表将等效于没有hasMany关系的表

b.) belongsTo , hasOne b。) 属于 ,具有hasOne

 class Man {                      
    ...                          
    Wife wife                    
    hasOne = [Hobby]             
    belongsTo = [People]         
 }

| wife_id | hoddy_id | people_id |

There will be id (BIGINT) in Man's table : " people_id " with key reference to People table, hasOne is similar to belongsTo and store key reference to Hobby table. Man的表中将有id (BIGINT):“ people_id ”,其键引用到People表, hasOne类似于belongsTo ,并将键引用存储到Hobby表。 ( Wife will be simmilar to hasOne = [Wife]) 妻子将类似于hasOne = [妻子])

if there will be no hasMany = [men : Man] in People table, there will be additional table: 'man_people'(or smth like that) 如果在People表中没有hasMany = [men : Man] ,则将有其他表:'man_people'(或诸如此类)

| man_id | people_id |

Fore more info about table relations you can find >> Here << . 有关表关系的更多信息,请参见>>这里<<

By the way you can change default data type in your mapping, for example : 顺便说一下,您可以在映射中更改默认数据类型,例如:

  name type: 'text'

Name will be LongText 名称将为LongText

PS Mb i missed smth, but i hope this will be small "cheat sheet" guide for you. PS Mb我错过了一些东西,但是我希望这对您来说是个小的“备忘单”指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM