简体   繁体   中英

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.

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. I also understand that this is based on Hibernate so I've tried searching there as well. 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)

This is some examples of default data types ->

1.) Simple data types :

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 )

a.) hasMany :

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

As there many men in people that is why we store reference key in Man domain. ( This mean people table in db will be equivalent to table without hasMany relation )

b.) belongsTo , 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. ( Wife will be simmilar to hasOne = [Wife])

if there will be no hasMany = [men : Man] in People table, there will be additional table: 'man_people'(or smth like that)

| 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

PS Mb i missed smth, but i hope this will be small "cheat sheet" guide for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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