The normal way to represent this relationship in grails would be:
public class GroupProfile {
static hasMany = [groupMembers: GroupMember]
}
This will automatically generate a method on GroupProfile
called addToGroupMembers
that will create the associations. Saving the GroupProfile
will also cascade to the group members, so you'll only need to call save once after adding the members.
Note that the groupMembers
collection will actually be an instance of Set
that doesn't necessarily preserve order. You can explicitly declare the collection as a List
to preserve order, but it requires additional overhead including an extra database column, so make sure that's really what you want.
Check out the grails manual in the section on GORM: http://grails.org/doc/latest/guide/GORM.html