I’m a beginner in Grails development. I want to make a new simple MySQL CRUD application which has repeatable fields in it. Now I’m not sure how my domain should look like. Let’s say I have 3 fields in my domain:
String term
String synonym
String author
I want to make author and/or synonym repeatable: is the correct way to proceed to make two new domain classes Synonym and Author and put this in my original domain:
static hasMany = [synonym: Synonym]
static hasMany = [author: Author]
It seems to me Grails would have a more clever way to do this, but maybe I’m just expecting too much..
What you name “repeatable” is commonly called a one-to-many relation. One term has many authors and many synonyms.
So far what you do looks correct.
Though I’d like to suggest to name the
hasManydeclarations in a plural form as they are holding multipleSynonyms/Authors:As a consequence of the
hasManydefinition you also have to think about the other side of the relationships. An author may also have many terms (many-to-many). For the synonym you have to evaluate if you want to allow to reuse the same Synonym for multiple terms or if it stands for one single term only (one-to-many vs many-to-many).