Skip to content

GPMONGODB-3: Exception when saving list of embedded objects #529

@graemerocher

Description

@graemerocher

Original Reporter: jcyboski
Environment: OSX 10.6.6 Java: 1.6.0_22 Groovy: 1.7.6 Grails: 1.3.6 Plugins: tomcat-1.3.6, mongodb-1.0-M4 (no hibernate plugin)
Version: Not Specified
Migrated From: http://jira.grails.org/browse/GPMONGODB-3

Trying to save a list of embedded objects (non-basic types). Using a single embedded document works fine.

Summary: Domain classes Person, and Address. A person can have a list of addresses.
When using an Array such as Address[] otherAddresses I receive NPE:
{code}
java.lang.NullPointerException
at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:139)
at org.grails.datastore.gorm.GormInstanceApi$save$0.callCurrent(Unknown Source)
at org.grails.datastore.gorm.GormInstanceApi$save$0.callCurrent(Unknown Source)
at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:87)
at org.grails.datastore.gorm.GormEnhancer$1.call(GormEnhancer.groovy:93)
at emb.PersonController$_closure1.doCall(PersonController.groovy:26)
at emb.PersonController$_closure1.doCall(PersonController.groovy)
at java.lang.Thread.run(Thread.java:680)
{code}
When using an List such as List

otherAddresses: I recieve IllegalArgumentException:
{code}
java.lang.IllegalArgumentException: can't serialize class emb.Address
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:205)
at org.bson.BSONEncoder.putIterable(BSONEncoder.java:230)
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:179)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:121)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:67)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:215)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:180)
at java.lang.Thread.run(Thread.java:680)
{code}

Below are the entire Domain classes and a Controller I used for testing.

Address.groovy:
{code}
package emb

class Address {
String address1

String city
String state

static constraints = {
}
}
{code}

Person.groovy
{code}
package emb

class Person {
Long id

String firstName
String lastName
Integer age

Address homeAddress
Address workAddress

List

otherAddresses
// Address[] otherAddresses

static embedded = ['homeAddress', 'workAddress', 'otherAddresses']

static constraints = {
homeAddress(nullable: true, blank: true)
workAddress(nullable: true, blank: true)
otherAddresses(nullable: true)
}
}
{code}

PersonController.groovy
{code}
package emb

class PersonController {

def index = {

  Person person = new Person(firstName: "Homer", lastName: "Simpson", age: 33)

  person.homeAddress = new Address(address1:" one main st.",
                               city: "springfield", state: "MA")

  person.workAddress = new Address(address1:"one way blvd.",
          city: "new york", state: "NY")

  Address addr1 = new Address(address1:"2222 6th ave.",
          city: "new york", state: "NY")

  Address addr2 = new Address(address1:"5555 6th ave.",
          city: "new york", state: "NY")

  person.otherAddresses = [addr1, addr2]


  if(person.validate() && !person.hasErrors()){

    person.save()
    [message: "Saved"]

  }else{
    [message: "Error", errors: [person.errors]]
  }


}

}
{code}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions