Announcement Announcement Module
Collapse
No announcement yet.
pushAll doesnt work with mongoMapper ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • pushAll doesnt work with mongoMapper ?

    When i did a push, it works fine :
    Code:
    update = update.push("persons", new Person("albert"));
    But when i did this :
    Code:
    update = update.pushAll("persons", new Object[] {new Person("albert"), new Person("kam")});
    Then, i get the exception :

    Code:
    INFO  [context.support.ClassPathXmlApplicationContext]: Refreshing org.springframework.context.support.ClassPathXmlApplicationCont
    ext@c135d6: startup date [Sat Mar 17 02:24:49 ICT 2012]; root of context hierarchy
    INFO  [factory.xml.XmlBeanDefinitionReader]: Loading XML bean definitions from class path resource [test-context.xml]
    INFO  [factory.support.DefaultListableBeanFactory]: Pre-instantiating singletons in org.springframework.beans.factory.support.Defa
    ultListableBeanFactory@111a903: defining beans [org.springframework.beans.factory.config.CustomEditorConfigurer#0,org.springframew
    ork.beans.factory.config.CustomEditorConfigurer#1,mongo,org.springframework.context.annotation.internalConfigurationAnnotationProc
    essor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalR
    equiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.beans.fact
    ory.config.CustomEditorConfigurer#2,mongoDbFactory,mappingContext,indexCreationHelper,mappingConverter,writeConcern,writeResultChe
    cking,ops,testThatFails,testThatWorks,test,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanP
    ostProcessor#0]; root of factory hierarchy
    Exception in thread "main" java.lang.IllegalArgumentException: can't serialize class kam.albert.domain.test.case2.Test$Person
    	at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:234)
    	at org.bson.BasicBSONEncoder.putArray(BasicBSONEncoder.java:246)
    	at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:206)
    	at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:140)
    	at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:190)
    	at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:140)
    	at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:86)
    	at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
    	at com.mongodb.OutMessage.putObject(OutMessage.java:142)
    	at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:346)
    	at com.mongodb.DBCollection.update(DBCollection.java:165)
    	at org.springframework.data.mongodb.core.MongoTemplate$12.doInCollection(MongoTemplate.java:851)
    	at org.springframework.data.mongodb.core.MongoTemplate$12.doInCollection(MongoTemplate.java:826)
    	at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:368)
    	at org.springframework.data.mongodb.core.MongoTemplate.doUpdate(MongoTemplate.java:826)
    	at org.springframework.data.mongodb.core.MongoTemplate.updateFirst(MongoTemplate.java:812)
    	at kam.albert.domain.test.case2.Test.test(Test.java:46)
    	at kam.albert.domain.test.case2.Test.main(Test.java:31)
    I tried multiple push within the same update, like this :
    Code:
    update = update.push("persons", new Person("albert")).push("persons", new Person("kam"));
    But this will result only the lastly-pushed-object being persisted, which is "kam" in this case. And i think this is the normal behavior anyway.

    Please share on anything that can be done ?

    Here is the complete source code :
    http://pastebin.com/yLg8NwxH

  • #2
    After looking at this solution : http://forum.springsource.org/showth...ltiple-pushAll
    I tried making an update subclass :

    Code:
    import org.springframework.data.mongodb.core.query.Update;
    
    public class MyUpdate extends Update {
    	public MyUpdate myPushAll(String key, Object[] values) {
    		super.addMultiFieldOperation("$pushAll", key, values);
    		return this;
    	}
    	public MyUpdate myPush(String key, Object[] values) {
    		super.addMultiFieldOperation("$push", key, values);
    		return this;
    	}
    }
    And now my code works :

    Code:
    MyUpdate update = new MyUpdate();
    
    // this works
    update.myPushAll("persons", new Object[] {new Person("albert"), new Person("kam")});
    
    // this works too
    //update.myPush("persons", new Object[] {new Person("albert"), new Person("kam")});
    I think i'll have to do with this workaround for now.

    Hopefully this will be supported in the future versions.

    Thanks !

    Comment


    • #3
      it happened to me also

      it happened to me also. it worked fine in M5, once i upgraded lib to 1.0 GA, it does not work any more, 1.0.1GA did not fix it also.
      i think it very common use case, SpringData team should support it directly.

      Comment

      Working...
      X