How to resolve having 2 belongsTo in a single domain
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 thi开发者_Python百科s:
static belongsTo = [ role: Role , company: Company ]
Since User-Role has m:n (also specified by the 'authorities' variable already).
Any other workaround? Thanks.
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!
精彩评论