Quantcast

Is it possible to do this in grails?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Is it possible to do this in grails?

rvanderwerf
2 domain classes, with 1 bidirectional relationship (w/collection) and 1 unidirectional relationship (single, pointing to each other):

public class User {
   String name
   String email
   String password
   String username
   UserContact contact
}

public class UserContact {
   static hasMany = [usersWhoOwnContact:User]

   User linkedUser

   static mapping = {
        usersWhoOwnContact joinTable: [name:'users_to_contacts',key:'contact_id',column:'user_id']        
   }

}



However, adding the relationship to User breaks the hasMany on the contact. When I tried to retrieve the hasMany relationship for 'usersWhoOwnContact', instead of looking in the join table, it looks for the '_idx' column in the User table. I'm not sure why it is confusing the 2 different relationships here. I've tried various uses of 'hasOne' and 'mappedBy'. It either erroneously thinks my hasMany is bi-directional, or fails during the lookup of the hasMany relationship.  Has anyone else encountered this and the proper scenario for modeling this?

I want the hasMany to use it's own join table. I want the bi-directional single relationship to use the columns in each table, or even better, just the column for the ID saved for it in the contacts table. It seems I can't have both. If I remove all relationships to Contact from User, the relationships on Contact seem to work fine.

Ryan
--

Ryan Vanderwerf
Lead Architect
KPI Consulting
512-218-1001 x601
http://www.kpi-consulting.net / http://www.developerprogram.com 


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Is it possible to do this in grails?

Paul Fernley
Am I correct in thinking that the 'real world' situation that you are trying to model is:

1. A user is assigned a single contact.
2. A user may be assigned as the contact for zero or more other users.
3. A user cannot be their own contact.

BTW, it seems like you will probably need to allow for a user having no contact, otherwise you would never be able to enter the very first user.

Regards,

Paul
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Is it possible to do this in grails?

rvanderwerf
In reply to this post by rvanderwerf
Thanks for your response!

1. yes - a user can be assigned to a contact (or none at all)
2. yes - but basically for admin users. I.e. a user may 'own' 1 or more contacts
3. no - a user can be their own contact. This is giving me the linkage of a user->contact.

The problem is I am having 2 different purpose linkages for a user to a contact. One for a user to 'own' a group of contacts, the other is to associate a user to a contact. It seems grails is confused by this, if the user <-> contact is bidirectional, and the 'owning' contact relationship is unidirectional.

As far as I can tell Grails doesn't allow this: I ended up having to move one of the relationships to an intermediate object and link it up there - not as clean, but I suppose this is one of those areas Grails just can't handle.


Ryan
----- Original Message -----
From: "Paul Fernley" <[hidden email]>
To: [hidden email]
Sent: Tuesday, November 1, 2011 7:22:48 AM GMT -06:00 US/Canada Central
Subject: [grails-user] Re: Is it possible to do this in grails?

Am I correct in thinking that the 'real world' situation that you are trying
to model is:

1. A user is assigned a single contact.
2. A user may be assigned as the contact for zero or more other users.
3. A user cannot be their own contact.

BTW, it seems like you will probably need to allow for a user having no
contact, otherwise you would never be able to enter the very first user.

Regards,

Paul


--
View this message in context: http://grails.1312388.n4.nabble.com/Is-it-possible-to-do-this-in-grails-tp3961391p3963315.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



--






Ryan Vanderwerf
Lead Architect
KPI Consulting
512-218-1001 x601
http://www.kpi-consulting.net / http://www.developerprogram.com 



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Loading...