tags:

views:

123

answers:

1

I have the following domains: User, Role, Company. User and Role has m:n relationship, Company to User has 1:m, and User to Company is 1:1. I'm having problem with the definition of User domain. Here it is:

class User {
 static hasMany = [authorities: Role ]
 static belongsTo = [ Role , Company ]
}

I would like to access the company from a User so that user.company will give me the company where he is assigned. This modification is not allowed:

static belongsTo = [ Role , company: Company ]

Here's the error:

Unexpected node type: EXPR found when expecting type: LABELED_ARG at line: 9 column: 41. File: /Users/alfred/Applications/grails_projects/extramile/grails-app/domain/fbm/extramile/User.groovy @ line 9, column 41.

Note that it is not an option to also do this:

static belongsTo = [ role: Role , company: Company ]

Since User-Role has m:n (also specified by the 'authorities' variable already).

Any other workaround? Thanks.

A: 

If I understand you correctly, this should work:

class User {
    static hasMany = [authorities: Role ]
    static belongsTo = [ Role , Company ]
    Company company
}

I can't help thinking that it's odd that your User belongs to a Company and not the other way around, i.e. deleting a Company deletes all Users in that company. Anyway, I don't know your domain so I'll shut up!

Dave
yes, this is what i did after posting my question and it did work as what i've planned. i'm supposed to answer my own question again but forgot to do it yesterday :p but still, thanks for the time :)
firnnauriel
btw, regarding your concern about the domain, is that how 'belongsTo' work? i added that because i would like to expose the dynamic methods (addTo* and removeFrom*).
firnnauriel
You only need addTo and removeFrom for collections so that GORM will take care of the setting of refs and cascading. The Grails docs give a fuller explanation of the consequences of using belongsTo.
Dave