Grails MongoDb embedded collection causes nullpointerexception

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Grails MongoDb embedded collection causes nullpointerexception

Jacob Severson

(Also posted this on stackoverflow)

I have 2 domain classes. One is the User class that acts as the user within spring security plugin and the other is a domain that embeds that User collection. Example:

@EqualsAndHashCode(includes=["email"])
@ToString(includeNames = true, includeFields = true, includes = 'email')
class User {
    String email
    Set<Role> authorities
    boolean enabled
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired
    static embedded = ['authorities']

    static transients = ['springSecurityService']

    static constraints = {
        email blank: false, nullable: false, unique: true
    authorities nullable: true
    }

    static mapping = {
        email index: true
    }
}


@EqualsAndHashCode(includes=["token"])
@ToString(includeNames = true, includeFields = true, includes = 'token')
class UserToken {

    User user
    String token
    Boolean unused
    Date dateCreated
    static embedded = ['user']

    static constraints = {
    user nullable: false, unique: true
    token nullable: false, unique: true
    unused nullable: false
    }

    static mapping = {
    token index: true
    }
}

In my tests, whenever I try to create and save an instance of UserToken I get this stack trace:

java.lang.NullPointerException at org.grails.datastore.mapping.model.types.Association.getInverseSide(Association.java:71) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedInstance(NativeEntryEntityPersister.java:1169) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedToOne(NativeEntryEntityPersister.java:1131) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.persistEntity(NativeEntryEntityPersister.java:932) at org.grails.datastore.mapping.engine.EntityPersister.persist(EntityPersister.java:132) at org.grails.datastore.mapping.core.AbstractSession.persist(AbstractSession.java:477) at org.grails.datastore.gorm.GormInstanceApi.doSave(GormInstanceApi.groovy:166) at org.grails.datastore.gorm.GormInstanceApi.save_closure4(GormInstanceApi.groovy:143) at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:302) at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:34) at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:142) at org.grails.datastore.gorm.InstanceMethodInvokingClosure.call(GormEnhancer.groovy:261) at com.datarecognitioncorp.security.UserTokenServiceSpec.save method should persist UserToken instance(UserTokenServiceSpec.groovy:26)

null java.lang.NullPointerException at org.grails.datastore.mapping.model.types.Association.getInverseSide(Association.java:71) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedInstance(NativeEntryEntityPersister.java:1169) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedToOne(NativeEntryEntityPersister.java:1131) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.persistEntity(NativeEntryEntityPersister.java:932) at org.grails.datastore.mapping.engine.EntityPersister.persist(EntityPersister.java:132) at org.grails.datastore.mapping.core.AbstractSession.persist(AbstractSession.java:477) at org.grails.datastore.gorm.GormInstanceApi.doSave(GormInstanceApi.groovy:166) at org.grails.datastore.gorm.GormInstanceApi.save_closure4(GormInstanceApi.groovy:143) at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:302) at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:34) at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:142) at org.grails.datastore.gorm.InstanceMethodInvokingClosure.call(GormEnhancer.groovy:261) at ...UserTokenServiceSpec.save method should persist UserToken instance(UserTokenServiceSpec.groovy:26)

Whenever I remove the embedded User from the UserToken everything goes back to working as expected.

I've been fiddling with it and am trying to debug through the source with no luck yet...anyone see something wrong?

Reply | Threaded
Open this post in threaded view
|

Re: Grails MongoDb embedded collection causes nullpointerexception

jstell
Are you sure you want to embed User in UserToken? Could you just put the user ID in UserToken? Embedding implies a "belongsTo" relationship, which doesn't seem to be what you want. Alternatively, you could just embed UserToken in User, which does make sense.

Jason


On Mon, Apr 29, 2013 at 11:07 AM, Jacob Severson <[hidden email]> wrote:

(Also posted this on stackoverflow)

I have 2 domain classes. One is the User class that acts as the user within spring security plugin and the other is a domain that embeds that User collection. Example:

@EqualsAndHashCode(includes=["email"])
@ToString(includeNames = true, includeFields = true, includes = 'email')
class User {
    String email
    Set<Role> authorities
    boolean enabled
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired
    static embedded = ['authorities']

    static transients = ['springSecurityService']

    static constraints = {
        email blank: false, nullable: false, unique: true
    authorities nullable: true
    }

    static mapping = {
        email index: true
    }
}


@EqualsAndHashCode(includes=["token"])
@ToString(includeNames = true, includeFields = true, includes = 'token')
class UserToken {

    User user
    String token
    Boolean unused
    Date dateCreated
    static embedded = ['user']

    static constraints = {
    user nullable: false, unique: true
    token nullable: false, unique: true
    unused nullable: false
    }

    static mapping = {
    token index: true
    }
}

In my tests, whenever I try to create and save an instance of UserToken I get this stack trace:

java.lang.NullPointerException at org.grails.datastore.mapping.model.types.Association.getInverseSide(Association.java:71) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedInstance(NativeEntryEntityPersister.java:1169) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedToOne(NativeEntryEntityPersister.java:1131) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.persistEntity(NativeEntryEntityPersister.java:932) at org.grails.datastore.mapping.engine.EntityPersister.persist(EntityPersister.java:132) at org.grails.datastore.mapping.core.AbstractSession.persist(AbstractSession.java:477) at org.grails.datastore.gorm.GormInstanceApi.doSave(GormInstanceApi.groovy:166) at org.grails.datastore.gorm.GormInstanceApi.save_closure4(GormInstanceApi.groovy:143) at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:302) at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:34) at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:142) at org.grails.datastore.gorm.InstanceMethodInvokingClosure.call(GormEnhancer.groovy:261) at com.datarecognitioncorp.security.UserTokenServiceSpec.save method should persist UserToken instance(UserTokenServiceSpec.groovy:26)

null java.lang.NullPointerException at org.grails.datastore.mapping.model.types.Association.getInverseSide(Association.java:71) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedInstance(NativeEntryEntityPersister.java:1169) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedToOne(NativeEntryEntityPersister.java:1131) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.persistEntity(NativeEntryEntityPersister.java:932) at org.grails.datastore.mapping.engine.EntityPersister.persist(EntityPersister.java:132) at org.grails.datastore.mapping.core.AbstractSession.persist(AbstractSession.java:477) at org.grails.datastore.gorm.GormInstanceApi.doSave(GormInstanceApi.groovy:166) at org.grails.datastore.gorm.GormInstanceApi.save_closure4(GormInstanceApi.groovy:143) at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:302) at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:34) at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:142) at org.grails.datastore.gorm.InstanceMethodInvokingClosure.call(GormEnhancer.groovy:261) at ...UserTokenServiceSpec.save method should persist UserToken instance(UserTokenServiceSpec.groovy:26)

Whenever I remove the embedded User from the UserToken everything goes back to working as expected.

I've been fiddling with it and am trying to debug through the source with no luck yet...anyone see something wrong?


Reply | Threaded
Open this post in threaded view
|

Re: Grails MongoDb embedded collection causes nullpointerexception

Jacob Severson
That is something I have thought about...I have been sticking to this relationship because of the use cases surrounding the UserToken object, however either way would technically work. 

Regardless, this error is really bothering me since I have a very similar setup between 2 other domains within the project that work just fine. I must be doing something stupid somewhere in one of the domains.


On Mon, Apr 29, 2013 at 11:21 AM, Jason Stell <[hidden email]> wrote:
Are you sure you want to embed User in UserToken? Could you just put the user ID in UserToken? Embedding implies a "belongsTo" relationship, which doesn't seem to be what you want. Alternatively, you could just embed UserToken in User, which does make sense.

Jason


On Mon, Apr 29, 2013 at 11:07 AM, Jacob Severson <[hidden email]> wrote:

(Also posted this on stackoverflow)

I have 2 domain classes. One is the User class that acts as the user within spring security plugin and the other is a domain that embeds that User collection. Example:

@EqualsAndHashCode(includes=["email"])
@ToString(includeNames = true, includeFields = true, includes = 'email')
class User {
    String email
    Set<Role> authorities
    boolean enabled
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired
    static embedded = ['authorities']

    static transients = ['springSecurityService']

    static constraints = {
        email blank: false, nullable: false, unique: true
    authorities nullable: true
    }

    static mapping = {
        email index: true
    }
}


@EqualsAndHashCode(includes=["token"])
@ToString(includeNames = true, includeFields = true, includes = 'token')
class UserToken {

    User user
    String token
    Boolean unused
    Date dateCreated
    static embedded = ['user']

    static constraints = {
    user nullable: false, unique: true
    token nullable: false, unique: true
    unused nullable: false
    }

    static mapping = {
    token index: true
    }
}

In my tests, whenever I try to create and save an instance of UserToken I get this stack trace:

java.lang.NullPointerException at org.grails.datastore.mapping.model.types.Association.getInverseSide(Association.java:71) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedInstance(NativeEntryEntityPersister.java:1169) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedToOne(NativeEntryEntityPersister.java:1131) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.persistEntity(NativeEntryEntityPersister.java:932) at org.grails.datastore.mapping.engine.EntityPersister.persist(EntityPersister.java:132) at org.grails.datastore.mapping.core.AbstractSession.persist(AbstractSession.java:477) at org.grails.datastore.gorm.GormInstanceApi.doSave(GormInstanceApi.groovy:166) at org.grails.datastore.gorm.GormInstanceApi.save_closure4(GormInstanceApi.groovy:143) at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:302) at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:34) at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:142) at org.grails.datastore.gorm.InstanceMethodInvokingClosure.call(GormEnhancer.groovy:261) at com.datarecognitioncorp.security.UserTokenServiceSpec.save method should persist UserToken instance(UserTokenServiceSpec.groovy:26)

null java.lang.NullPointerException at org.grails.datastore.mapping.model.types.Association.getInverseSide(Association.java:71) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedInstance(NativeEntryEntityPersister.java:1169) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.handleEmbeddedToOne(NativeEntryEntityPersister.java:1131) at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.persistEntity(NativeEntryEntityPersister.java:932) at org.grails.datastore.mapping.engine.EntityPersister.persist(EntityPersister.java:132) at org.grails.datastore.mapping.core.AbstractSession.persist(AbstractSession.java:477) at org.grails.datastore.gorm.GormInstanceApi.doSave(GormInstanceApi.groovy:166) at org.grails.datastore.gorm.GormInstanceApi.save_closure4(GormInstanceApi.groovy:143) at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:302) at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:34) at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:142) at org.grails.datastore.gorm.InstanceMethodInvokingClosure.call(GormEnhancer.groovy:261) at ...UserTokenServiceSpec.save method should persist UserToken instance(UserTokenServiceSpec.groovy:26)

Whenever I remove the embedded User from the UserToken everything goes back to working as expected.

I've been fiddling with it and am trying to debug through the source with no luck yet...anyone see something wrong?



Reply | Threaded
Open this post in threaded view
|

Re: Grails MongoDb embedded collection causes nullpointerexception

aggiepilot04
I know this is super-old, but a Google search led me here whilst facing the same issue.  Make sure that you have added @Mock(User) to your unit test class.  In my case, I had multiple levels of embedded domain classes, and each of them had to be mocked.