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.,

class Person {

    static hasMany = [addresses: Address]

}

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

class Person {

    static hasMany = [names: String]

}

here grails create a join table Person_Names having a column Names_String with type VARCHAR and size 255.

Size of hasMany: Name column of 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

class Person {

    static hasMany = [names: String]

    static mapping = {
        names joinTable: [column: 'names', sqlType: 'varchar(32)']
    }

}

here only 32 characters length is assigned to the names.

Size of hasMany: Names column with size 32

Hope this helps 🙂 .