Working with "Spring Security Core Plugin" and application domain objects

11 views
Skip to first unread message

bjh

unread,
Feb 2, 2011, 12:31:28 PM2/2/11
to Utah Groovy & Grails User Group
Hi,

I have what is probably a simple question. I'm working with the
Spring Security Core Plugin. My application will create new
application objects in the database to be associated with each user.
The simple question is what is the proper/Grails way of making the
user/domain object relationship?

The two ways that I currently know about is using the "belongsTo"
specifier as in the following:

class Team {
static belongsTo= [ user : User]

...

}

And then in the 'save' method simply assigning

teamInstance.user = springSecurityService.currentUser


The other way would be to do the following:


class Team {
User user

...

}

And then in the 'save' method simply assigning

teamInstance.user = springSecurityService.currentUser


I guess another option would be to do this:

class Team {
Long userID

...

}

And then in the 'save' method simply assigning

teamInstance.userID = springSecurityService.principal.id


Is there is a preferred way of doing this? It seems like there
aught to be a way to inject the association automatically somehow as
well.

On a related note...Is it illegal to have more than one 'belongsTo'
association? For instance I need the 'belongsTo' for the User but
then need the normal domain associations as well. My many-to-many
associations seem to only allow for one 'belongsTo'.

Thanks,
Brent Hale

Eric Berry

unread,
Feb 2, 2011, 12:35:56 PM2/2/11
to ug...@googlegroups.com
I've always used the belongsTo and hasMany, with adding the User into the child domain attributes. For example:

class Team {

  static belongsTo = [user: User]

  User user
  ...
}

Then when I assign the user to the child, I just do this:

team.user = user




--
Utah Groovy & Grails Users Group
ug...@googlegroups.com
http://groups.google.com/group/uggug/
- All meeting times and places can be found here.

** Please prefix your subject with "[JOB]" if your message is about job opportunities.

Jeff scadden

unread,
Feb 2, 2011, 12:50:51 PM2/2/11
to ug...@googlegroups.com
Depending on how you want the database structure to be, and if you want cascading, you use a combination of:
User user
belongsTo
hasOne

The following documentation has been helpful in figuring out relationships:
Click on - 5. Object Relational Mapping (GORM)
Search one the page for "belongsTo", "hasOne", "Many-to-many", Many-to-one"
Reply all
Reply to author
Forward
0 new messages