Fabien Barbier
Fabien Barbier

Reputation: 1524

Zero-to-many relationship with Grails?

I have a tRNA class which may or may not have an associated grRNA, but will be associated to cRNA.

So I can have this relationship :
tRNA -> grRNA -> cRNA

Or this relationship (in this case we don't have grRNA data) :
tRNA -> cRNA

What is the best way to implement this relationship (we may not have grRNA sometimes) via Grails/Gorm ? Best domain class design ?

Upvotes: 1

Views: 925

Answers (1)

Igor
Igor

Reputation: 33993

You could have grRNA and cRNA be subclasses of a parent, with tRNA having an association with the parent, and grRN associating with cRNA.

In your database tables you would need a class column to define the class (discriminator in the GORM object).

Edit: Something like:

class GenericRna {
    //Properties
    //Assuming this is mapped to a database table as well, you'd need:
    static mapping = {
        table 'generic_rna'
        discriminator column: 'class'
    }
}

class CRna extends GenericRna {
    //Properties
    discriminator value: 'CRna'
}

class GrRna extends GenericRna {
    //Properties
    static hasMany = [cRnas: CRna]
    discriminator value: 'GrRna'
}

class TRna {
    static hasMany = [genericRnas: GenericRna]
}

Technically I believe that if you use 'class' as your discriminator column name, and the class names as the values, you do not need the 'discriminator' lines.

Upvotes: 1

Related Questions