Reduce size of hasMany property of type String
Grails follows MVC(Model View Controller) design pattern and domain classes fulfills the M in MVC. Each domain class mapped with a table in the database, i.e., a domain class represents a table in the database.
hasMany defines a one-to-many association between two classes, eg.,
1 2 3 4 5 6 7 |
[code] class Person { static hasMany = [addresses: Address] } [/code] |
here we define a one-to-many relationship between Person and Address classes where one Person has many Address.
We can also use String, Integer, etc for hasMany association, like
1 2 3 4 5 6 7 |
[code] class Person { static hasMany = [names: String] } [/code] |
here grails create a join table Person_Names having a column Names_String with type VARCHAR and size 255.
But what if we need small amount eg. 32 characters only. Then it is useless to assign 255 characters size for names.
We can solve this problem by using joinTable in mapping, like
1 2 3 4 5 6 7 8 9 10 11 |
[code] class Person { static hasMany = [names: String] static mapping = { names joinTable: [column: 'names', sqlType: 'varchar(32)'] } } [/code] |
here only 32 characters length is assigned to the names.
Hope this helps 🙂 .
Recent Comments