views:

718

answers:

2

Hopefully this will be an easy one to answer. I created a class in Grails called player which has this information:

class Player {
 String steamId
 String name
 String portrait
 static hasMany = {playerStatistics:PlayerStatistics}
 static hasOne = {playerForumProfile:PlayerForumProfile}
}

For clarification, a Player object can have one PlayerForumProfile object, but the player object is always created BEFORE the PlayerForumProfile object. My issue is with accessing the playerForumProfile object associated with the "hasOne" property within the controller of the PlayerForumProfile class. I had assumed that doing this:

    def playerForumProfileInstance = new PlayerForumProfile()
    def playerInstance = Player.get(params.id)

    playerForumProfileInstance = playerInstance.playerForumProfile

would result in pulling the PlayerForumProfile object associated with the playerInstance object into the playerForumProfileInstance variable, however when I try this, Grails throws an error telling me there is no such property as playerForumProfile. Is it possible to access the hasOne properties' object in such a manner or do I need to do something else?

Edit: I also tried modifying the Player class so it included a variable called playerForumProfile and editing PlayerForumProfile so it had a belongsTo declaration, but this kept resulting in a null pointer exception when running my app.

Edit: A little more info, I created a new grails app from scratch and created the relationship the way it appears in the Grails documentation and it ran with no problem so I'm thinking it may be easier just to start a new app and copy the files over.

+3  A: 

There isn't a "hasOne" property in GORM, it's either belongsTo:

static belongsTo = [playerForumProfile: PlayerForumProfile]

or just a regular typed definition of the attribute name if there isn't a cascading relationship implied by belongsTo:

PlayerForumProfile playerForumProfile

See the one-to-one GORM documentation for details.

Ted Naleid
I've checked the documentation and what I'm saying is when I try their examples and run my application I get a null pointer when placing a PlayerForumProfile object (as a property) in the Player class.
Neitherman
Techically this is the correct answer.
Neitherman
A: 

There is hasOne feature in GORM: http://grails.org/doc/latest/ref/Domain%20Classes/hasOne.html

amra