abulhol
abulhol

Reputation: 171

belongsTo array plus hasMany ownership

I have a Grails class that both has got a many-to-many relationship (with the other side being the owner) as well as a n:1 relationship.

I could not find an answer on the web how to put the ownership into the belongsTo clause.

This is the code:

class PanelType { 
    static hasMany = [elements: LabValueType] 
} 

class LabValueType { 

    static belongsTo = [labUnit: LabUnit] 
    // This is what would be needed to have a bidirectinal n:m relationship 
    // belongsTo = PanelType 
    // static hasMany = [panelTypes: PanelType] 
} 

If I leave it like this, the application builds the database correctly, but I won't be able to navigate from LabValueType to PanelType.

I found one answer (from 2008!) that said I should write: static belongsTo = [PanelType, LabUnit] BUT this way, the field lab_unit_id is not created in the database, so it does not seem to be correct.

Upvotes: 0

Views: 68

Answers (1)

abulhol
abulhol

Reputation: 171

I have found that I can work around this problem by declaring the relationships like this:

LabUnit labUnit

static belongsTo = PanelType
static hasMany = [panelTypes: PanelType]

But somehow it is not really 100% satisfying.

Upvotes: 0

Related Questions