I have 2 domain classes which are mapped by many-to-many relationship. I followed the instruction of Grails documentation, but I still have some problem when processing data on those domains. Here are my 2 domain classes:
class User {
String name
int age
String job
static hasMany = [groups : Group]
static belongsTo = [org : Organization]
}
class Group {
String groupName
String code
static hasMany = [members : User]
}
My problems are:
1. The above relationship require one class hold belongsTo to be the "owner" of the relationship. In this context, the User belongs to the Group, but I do not know how to put the belongsTo to User class, because the standard syntax that Grails suggest is static belongsTo = [Group] (just specify the owner class name), so I cannot:
- put it into the exist belongsTo like this: static belongsTo = [org : Organization, Group]
- or define another belongsTo like this: static belongsTo = [Group]
Is below example right:
class Book { String title static belongsTo = Author static hasMany = [authors:Author]
static mapping = { authors joinTable:[name:"mm_author_books", key:'mm_book_id' ] }
} class Author { String name static hasMany = [books:Book]
static mapping = { books joinTable:[name:"mm_author_books", key:'mm_author_id'] }
}
(Ref link: Many-to-Many link tables in grails (GORM) / hibernate)
I mean that do we need to specify the name of foreign key of the join table for each class?
- If I want to find all User that are members of a specified Group whose name is "ABC", how can I use the DynamicFinder of Grails?
Thank you so much