This is my first project with many-to-many relationship and after reading some grails stuff of ORM, I decided to let grails do the work for me, like creating the tables. Actually, my deal was:
1 User has many Groups
1 Group belongs to an User and has many Users(except the owner)
Here are my classes:
class User {
String name
List groups
static hasMany = [groups : Group]
static mapping = {
table 'users'
version false
}
}
class Group {
String name
List members
static belongsTo = User
static hasMany = [members : User]
static mapping = {
table 'groups'
version false
}
}
After that, what I got from grails was 3 tables. The users table was ok, but the next 2 wasn't what I expected.
table **groups** | id | name
table **users_groups** | group_id | user_id | members_idx | groups_idx
Well, what I wanted was something like this:
table **groups** | id | name | user_id
table **users_groups** | group_id | members_idx | groups_idx
I don't know if I did something wrong or how can I fix it, but anyway... I'm still getting another problem when I try to do user.addToGroups(new Group()) . My users_groups table duplicates this register, one with the members_idx null, and the other register with the groups_idx null.
How about removing belongsTo from group and leave hasMany:
class User {
String name
List groups
static hasMany = [groups : Group]
static mapping = {
table 'users'
version false
}
}
class Group {
String name
List members
static hasMany = [members : User]
static mapping = {
table 'groups'
version false
}
}
EDIT
I think You have to make a domain class which will actually join groups and users.
I think You should try this:
class User {
String name
static hasMany = [groups : UsersGroups]
static mapping = {
table 'users'
version false
}
}
class Group {
String name
static hasMany = [members : UsersGroups]
static mapping = {
table 'groups'
version false
}
}
class UsersGroups {
static belongsTo = [group: Group, user: User]
static mapping = {
version false
}
}
It will create another table which actually joins groups and users.
If you change the belongsTo to use Map syntax it will use a foreign key and not a join table:
static belongsTo = [user: User]
and you'll have a user field as a back-reference.
I have done a similar issue before, so I think this may work:
class User {
String name
hasMany = [groups : Group]
static mapping = {
table 'users'
version false
}
}
class Group {
String name
static belongsTo = [owner : User]
static hasMany = [members : User]
static mapping = {
table 'groups'
version false
}
}
About the problem with user.addToGroups, I think that you should set the owner when creating the group first. Then add other users to group.
Related
I have domain classes as below:
class Account {
String name
String uniqueName
static hasMany = [roles:Role]
}
class Role {
String name
static belongsTo = [account:Account]
static hasMany = [users: User]
}
class User {
String name
}
I received Account's uniqueName from params.uniqueName. And I want to find all users list that has roles that belongsTo account.
I want to use criteria() because I want to do it in pagination.
I try like below code, it's work but it can't do a pagination.
def account = Account.findByUniqueName(params.uniqueName)
def roles = account.roles
[users : roles.users.flatten().unique()]
How should I do this?
Try this one:
add to user:
static belongsTo = [role:Role]
and use this criteria:
User.createCriteria().listDistinct {
role{
account{
eq("uniqueName", params.uniqueName)
}
}
}
You can use criteria like this.
List<Account> results =Account.createCriteria().list
{
eq('uniqueName', params.uniqueName)
maxResults(params.max as int)
firstResult(params.offset as int)
order(params.order, "asc")
}
Try this:
Account account = Account.findByUniqueName(params.uniqueName)
Collection users = []
account.roles.each{ role ->
users += role.users
}
log.info("Users for ${params.uniqueName} account : ${users}"
My project requires me to maintain the insertion and retrieval order in a many-many relationship. By default, groovy saves the elements as a Set in many-many relationship. I want to do it in a List. I am not sure how to update the relationship to use List instead of Set. Any help would be appreciated.
class Course{
static belongsTo = Teacher
static hasMany = [teacher:Teacher]
static mapping = {
teacher joinTable : [name: TeacherCourse]
}
}
class Teacher{
static hasMany = [course:Course]
static mapping = {
course joinTable : [name: TeacherCourse]
}
}
save() call on either Teacher or Course also inserts a new row in TeacherCourse table. It works with no issues. In Database there the tables are:-
Teacher (PK: Id)
Course (PK: Id)
TeacherCourse(PK: [Teacher_id,Course_id])
Is there a way I can maintain the order of insertion and retrieval in many-many relationship?
Thank you..
Edit
In controller save()
def courseInstance = new Course()
List <Teacher> teacherList= []
teacherList.add(Teacher.findById(65))
teacherList.add(Teacher.findById(36))
courseInstance.courseUnits = teacherList
courseInstance.save(flush:true)
Try this:
class Course {
List teachers
static belongsTo = Teacher
static hasMany = [teachers:Teacher]
static mapping = {
teachers joinTable : [name: TeacherCourse]
}
}
class Teacher {
List courses
static hasMany = [courses:Course]
static mapping = {
courses joinTable : [name: TeacherCourse]
}
}
Reference
I read that a m:m relationship often means there is a third class that isn't yet required. So I have m:m on User and Project, and I created a third domain class, ProjectMembership
The three domains are as follows (minimized for illustration purposes):
User
class User {
String name
static hasMany = [projectMemberships : ProjectMembership]
}
Project Membership
class ProjectMembership {
static constraints = {
}
static belongsTo = [user:User, project:Project]
}
Project:
class Project {
String name
static hasMany = [projectMemberships : ProjectMembership]
static constraints = {
}
}
If I have the ID of the user, how can I get a list of Project objects that they are assigned to?
There are a handful of ways - here are a couple:
def user = User.get(userId)
ProjectMembership.findAllByUser(user).collect { it.project }
or to avoid the query for the User:
ProjectMembership.withCriteria {
user {
eq('id', userId)
}
}.collect { it.project }
Be wary of queries that'll return large result sets - you'll end up with a huge in-memory list of project objects.
I have a relationship between two domain class: User and Bank, user has many of bank and bank has many of user, the result table I call it as mm_user_banks that keep both table IDs for MM relationship.
class User {
String name
String password
Date createdAt = new Date()
Date loginAt
static hasMany = [banks:Bank]
static fetchMode = [banks: 'eager']
static constraints = {
}
static mapping = {
banks joinTable: [name: 'mm_user_banks', key: 'mm_user_id']
}
}
and Bank...
class Bank {
Long id
String name
static belongsTo = User
static hasMany = [users:User]
static constraints = {
}
static mapping = {
id generator: 'assigned'
users joinTable: [name: 'mm_user_banks', key: 'mm_bank_id'], lazy: false
}
}
I can save users with banks as well, but the problem is when I try to retrieve bank list from an user instance and it only fetch one row even if there is more than one row for this user in mm_user_banks table.
E.g:
User u = new User(name: 'user', password: 'pwd')
u.addToBanks(Bank.findById(1))
u.addToBanks(Bank.findById(2))
u.addToBanks(Bank.findById(3))
u.save(flush: true)
In database it is OK, fetching 3 rows, but when I get the user and see its bank list:
def user = User.findById(1) //OK
println "Size ${user.banks?.size()}" //only 1??? How?
Thanks in advance!
Just tested it out - "user.banks?.size()" returns "3". Are you sure that data in DB is OK?
Anyway, if you want to use eager fetching for collections then define it in "mapping" of User class (instead of "fetchMode" static field):
static mapping = {
banks joinTable: [name: 'mm_user_banks', key: 'mm_user_id'], fetch: 'join'
}
in Bank class use fetch: 'join' in mapping instead of lazy: false
Thanks so much, Sergei, but didn't worked out.
I fixed it in an ugly way, but this is was my remaining way out.
I created another domain class to mapping the MM table, so I did a mm_user_banks class. I use it to fetch records from bank or user and worked OK. To save banks for a user I actually do it in the old way as well, not changed from that. I guess this mighty not be the best way, but like I said, worked out.
I've tried "fetch: 'join'" and "lazy: false" and "static fetchMode = [banks: 'eager']" without success.
The new domain class is:
class UserBanks {
User user
Bank bank
static constraints = {
}
static mapping = {
table name: 'mm_user_banks'
user column: 'mm_user_id'
bank column: 'mm_bank_id'
}
}
I had the similar problem but in my case I had another relation between my equivalents to User and the Bank. This made the hasMany statement to use the wrong relation to map the many to many relation. In my case it was solved by adding a mappedBy statement to the relation:
class Bank{
...
static mappedBy=[users:'banks']
...
}
and
class User{
...
static mappedBy=[banks:'users']
...
}
or as it was in my case, since it was a uni-directional relation:
class ArticleGroup {
String name
static hasMany = [articles:Article]
static mappedBy = [articles: 'none']
static constraints = {
name(nullable:false, blank:false, unique:true, maxSize:100)
}
static mapping = {
table 'article_group'
sort 'name'
articles joinTable: [name:'article_group_article', key: 'articleGroup_id', column:'article_id'], fetch: 'join'
}
i have the following (simplified) domain classes
class Filter {
String name
static hasMany = [answers:Answer]
static belongsTo = [user:User]
}
class User {
String name
static hasMany = [answers:Answer, filters:Filter]
}
class Answer {
String text
}
Then i add answers to the user which is working perfectly. The problem occurs when i delete 1 answer of a user:
def delete = {
def answer = Answer.get(params.id)
def users = User.withCriteria() {
answers{
eq("id", answer.id)
}
}
for (user in users)
user.removeFromAnswers(answer)
answer.delete(flush:true)
redirect(action:"index")
}
What happens here is that ALL user --> answer associations get deleted.
I only want to delete this 1 answer and of cause all associations the answer is used.
I know this has to do with the missing belongsTo, but i can't use it because a ansswer can either belong to a user or to an filter...
You can add the belongsTo to set them to nullable:
class Answer {
String text
static belongsTo = [user:User, filter:Filter]
static constraints = {
user nullable:true
filter nullable:true
}
}
and then just delete the Answer directly in the Controller:
def delete = {
def answer = Answer.get(params.id)
answer.delete(flush:true)
}
GORM will take care of the rest the cascading for you.