accessing associations within GORM Events

Posted by Don on Stack Overflow See other posts from Stack Overflow or by Don
Published on 2010-03-09T03:27:43Z Indexed on 2010/03/09 3:36 UTC
Read the original article Hit count: 379

Filed under:
|
|

Hi,

My Grails app has a User class and a Role class. The User class has an authorities property which holds the roles assigned to that user, e.g.

class User {

    static hasMany = [authorities: Role]

    // This method is called automatically after a user is inserted
    def afterInsert() {
        this.authorities.size()
    }
}

If I create a user and assign them a role, a NullPointerException is thrown from the GORM event method afterInsert(), because authorities is null.

If I comment out afterInsert() the user is saved correctly along with the assigned role. Is there some reason why I can't access associations from GORM event methods. Is is possible that this event is fired after the User row is inserted, but before the row is added to the User-Role join table?

Thanks, Don

© Stack Overflow or respective owner

Related posts about grails

Related posts about gorm