3

Usually I'm using One-to-many relationship by this way :

class Study {
    static hasMany = [ crfs : Crf ]
    String name 
    ...
} 

class Crf { 
String title
String info 
...
} 

I can extend this relationship to others domains, Ex :

static hasMany = [ crfs : Crf, crfb : CrfBlood ...]

But in my case I have to link the Study domain to 30 others domains, maybe more...(ex : CrfBlood, CrfMedical, crfFamily, etc...).

What domain model implementation should I use in my case ?
I would like to keep the dynamic finders usability in my project.

Update - model complement :

A Study can have one-to-many Subject.
A Study can have one-to-many Crfs (ex : CrfBlood, CrfMedical, crfFamily, etc...).
A Subject can have one-to-many Visit (ex : a subject can have several Blood testing).

I would like to dynamically assign Crfs to a Study, so how can I use GORM (dynamic finders) without using static hasMany = [...] in my domain ?
Maybe, I can implement a service to do the same stuff did by hasMany ?

Fabien Barbier
  • 1,514
  • 4
  • 28
  • 41
  • 1
    What's the problem having 30 domain classes in `hasMany`? Are all the relationships 1:M (maybe `blood` is 1:1 relationship)? – Victor Sergienko Feb 17 '11 at 10:06
  • I could have for some crfs with 1:1 relationship. Usually it would be 1:M. – Fabien Barbier Feb 17 '11 at 16:50
  • so there's some "hasOne" relationship: http://grails.org/doc/latest/ref/Domain%20Classes/hasOne.html . But.. what's so bad with hasMany? Why don't you just use it? – Hoàng Long Feb 18 '11 at 02:11
  • Each time I'm adding a new crf, I have to update the Study domain. Also, for each Study an administrator must define which crfs he wants to use for the study (for example : Study1 only use CrfBlood and CrfMedical, whereas Study2 use only CrfMedical and CrfFamily). – Fabien Barbier Feb 18 '11 at 17:04

1 Answers1

0

You can declare all Crf types as subclasses of Crf, so that you'll only have one relationship to Study, but still be able to add the different types.

class Crf {
    String title
    String info
}

class CrfBlood extends Crf {
    String detailBlood
}

class CrfMedical extends Crf {
    String detailMedical
}

class Study {
    String name
    static hasMany = [ crfs: Crf ]
}

Then you can do:

def s = new Study(...)
def c1 = new CrfBlood(...)
def c2 = new CrfMedical(...)
s.addToCrfs(c1)
s.addToCrfs(c2)
Gustavo Giráldez
  • 2,610
  • 18
  • 12