Quantcast

Grails 2.0.1 Many-to-Many Mapping only maps once?

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

Grails 2.0.1 Many-to-Many Mapping only maps once?

Scott
In the Grails 2.0 User guide it gives an example of Many-to-Many Mapping
I.e.
 class Group {
    …
    static hasMany = [people: Person]
}

class Person {
    …
    static belongsTo = Group
    static hasMany = [groups: Group]
}

stating that "by default maps a many-to-many association using a join table."
My question is does this ensure that a person is only added to one group once?
if not, how can I define the domain relationship that allows  a user to join many groups but also ensure the jointable only maps the person to a particular group once?

I'm currently  using a groupRegistration class as a joining table and it also holds extra information. e.g. registrationDate. memberStatus. I know this will allow a user to register to the same group repeatedly. How can I prevent that?

class GroupRegistration {
       
    static constraints = {
        group()
        user()
        dateCreated()
    }
       
        Date dateCreated
        static belongsTo = [group:Group, user:User]
}

class Group {
    static constraints = {
                name(blank:false, maxSize:50)
                description(maxSize:4*1024, blank: false)
                visibility(inList:["Public", "GroupOnly", "RegisteredOnly"])
    }
        String name
        String description
        String visibility
        Date dateCreated

        static hasMany = [registrations:GroupRegistration]
}

class User {
        transient springSecurityService
        String username
        String password
        boolean enabled
        String email
        boolean accountExpired
        boolean accountLocked
        boolean passwordExpired
       
        static hasMany = [ groupRegistrations:GroupRegistration] ]
...
}
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Grails 2.0.1 Many-to-Many Mapping only maps once?

Ian Roberts
On 27/02/2012 13:35, Scott wrote:
> I'm currently  using a groupRegistration class as a joining table and it
> also holds extra information. e.g. registrationDate. memberStatus. I know
> this *will *allow a user to register to the same group repeatedly. How can I
> prevent that?

With a unique constraint - user(unique:'group') or vice versa.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
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

Re: Grails 2.0.1 Many-to-Many Mapping only maps once?

Scott
This post was updated on .
Tried that, works fine,
Now produces the following error message if same user attempts to register to a group they already belong to.  But also allows user to join many different groups.

Property [user] of class [class GroupRegistration] with value [Scott] must be unique

This is the kind of solution I was looking for,  My only remaining question now is performance wise how well does it scale when there are 100,000s of users and thousands of groups?
Given that there are many more (10-100x) users than groups is it better to specify the constraint as
user(unique:'group')   or group(unique:'user') ?

many thanks

Scott
Loading...