Grails code to model M-M relationship between Person & LinkedIn Group resolved

In my earlier post Trying not to be gormless with GORM I went through a series of painful steps to facilitate modelling the association between a Person and a LinkedInGroup.

That post is extensively cross-referenced highlighting often misleading documentation, but also the path to the solution.

This post summarises the results what I wanted to achieve.

Many people can be in a LinkedInGroup and a LinkedInGroup can contain many people.

If I wanted to remove a person from the database, I wanted to remove all their LinkedInGroup Membership records too.

The same goes if I delete a LinkedInGroup I want to remove all the  intermediary association table (member) records.

I included the assocation table in the model, so as to be able to have a dual belongsTo modelled to both LinkedInGroup and Person.

Here is my final solution. Many thanks again to Stephane Maldini for getting me started via the mailing list.

Person.groovy

LinkedInGroup.groovy

LinkedInGroupMember.groovy

Revision History:

1:

I created a follow-up post to this, and improved on things slightly by adding an index to facilitate better performing deletion of LinkedInGroups belonging to a Person, but GORM is rather lacking in this respect and I wasn’t able to create the index I wanted…
Here is the updated LinkedinMembers source:

LinkedInGroupMember.groovy v2

2:

Removed redundant properties. You don’t need them when belongs to follows the Map syntax. If you use the List syntax the underlying schema in you database would end up with 5 tables instead of three. I talked about this at length in the earlier post.

LinkedInGroupMember

PS: I’m still keen to understand why the Grails Console doesn’t recognise the addToPerson/addToLinkedInGroup from LinkedInGroupMember instances.

Advertisements

About this entry