Spring Data MongoDB
  1. Spring Data MongoDB
  2. DATAMONGO-374

Custom Converter not used when doing $set updates

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Cannot Reproduce
    • Affects Version/s: 1.0 GA
    • Fix Version/s: None
    • Component/s: Core, Mapping
    • Labels:
    • Environment:

      Description

      Short summaries before the simple examples :

      • Inserting new documents is OK, in the meaning that it uses my custom converter
      • Updating an existing document is not OK, in the meaning that it doesnt use my custom converter

      Here's my class that has the it's own converter

      public class MyClass extends MyInterface<String> ...
      

      Here are the the converters :

      public class MyClassWriteConverter implements Converter<MyClass, DBObject> {
          @Override
          public DBObject convert(MyClass myClass) {
          	System.out.println("MyClass WRITE CONVERTER !");
          	DBObject dbo = new BasicDBObject();
          	dbo.put("title", myClass.getTitle());
          	dbo.put("value", myClass.getValue());
          	System.out.println("value : " + myClass.getValue());
          	System.out.println("class : " + myClass.getClass().getCanonicalName());
          	dbo.put("_class", myClass.getClass().getCanonicalName());
          	return dbo;
          }
      }
      
      public class MyClassElementReadConverter implements Converter<DBObject, MyClass> {
          @Override
          public MyClass convert(DBObject dbObject) {
          	String value = (String) dbObject.get("value");
          	String title = (String) dbObject.get("title");
          	return new MyClass(title, value);
          }
      }
      
      <mongo:converter>
        <bean class="kam.albert.MyClassElementReadConverter" />
      </mongo:converter>
      <mongo:converter>
        <bean class="kam.albert.MyClassElementWriteConverter" />
      </mongo:converter>
      

      myDomain has a List<MyClass<?>> myClasses,
      and this works fine, as the custom converter is being used :

      this.ops.save(myDomain, "myCollection");
      

      Which can be seen from the debugging output :

      MyClass ELEMENT WRITE CONVERTER !
      value : my value
      class : kam.albert.MyClass
      

      And has the result from my db.myCollection.find().pretty(); :

      {myClasses : [
         {"title" : "my title",
          "value" : "my value",
          "_class" : "kam.albert.MyClass"}
         ]
      }
      

      All is still OK with the insert operation. But when i make $set update to an existing document like this :

      this.ops.updateFirst(
         this.idCriteria(myClass),
         new Update()
             // set the content node
             .set(dotNotation, myClass),
             "myCollection"
         );
      

      The debugging output stays the same :

      MyClass ELEMENT WRITE CONVERTER !
      value : my value
      class : kam.albert.MyClass
      (edited for clarity, the other properties omitted)
      DEBUG [mongodb.core.MongoTemplate]: calling update using query: { "_id" : "81d3292e-fd75-410d-a1f9-b109b6d76194"} and update: { "$set" : { "myClasses" : [ { "title" : "my title" , "value" : "my value"}]}] } in collection: myCollection
      

      But the result is like without using the custom converter, without the _class attribute :

      {myClasses : [
          {"title" : "my title",
           "value" : "my value",
          }
      ]}
      

      My current conclusions are :

      • For inserting a new document, the custom converter works fine
      • For updating though, although the custom converter seems to be called (the debugging output is there), but it seems to be using
        the default MappingMongoConverter to do the real update.

      Basically, i just want my custom converter to be used in $set updates ..

      Thank you !

        Activity

        Hide
        Oliver Gierke added a comment - - edited

        Please have a look at the just commited test case that shows a custom converter being used for updates. I've slightly polished the implementation of MongoTemplate.doUpdate(…) along the way but the test case was working with the prior implementation as well. However feel free to run your tests against the just deployed snapshot build to verify whether the polishing might have changed behavior for your scenario.

        Show
        Oliver Gierke added a comment - - edited Please have a look at the just commited test case that shows a custom converter being used for updates. I've slightly polished the implementation of MongoTemplate.doUpdate(…) along the way but the test case was working with the prior implementation as well. However feel free to run your tests against the just deployed snapshot build to verify whether the polishing might have changed behavior for your scenario.
        Hide
        albert kam added a comment -

        I feel really bad for not making a simple example to test this situation i had. Now the sources have changed so much that i am now having difficulties to reproduce the problem. I am currently sticking as much as possible to the default mongoMapping without providing my own converters. Hopefuly i can give feedback in the future.

        Thanks a lot for the help !

        Show
        albert kam added a comment - I feel really bad for not making a simple example to test this situation i had. Now the sources have changed so much that i am now having difficulties to reproduce the problem. I am currently sticking as much as possible to the default mongoMapping without providing my own converters. Hopefuly i can give feedback in the future. Thanks a lot for the help !
        Hide
        Oliver Gierke added a comment -

        Closing due to inactivity.

        Show
        Oliver Gierke added a comment - Closing due to inactivity.
        Hide
        Nikita Sudan added a comment -

        I am seeing this problem in spring data 1.2.1.RELEASE. Looks like the link to the above test case is broken. Could you please re-post this link? Thanks a lot in advance!!!

        Show
        Nikita Sudan added a comment - I am seeing this problem in spring data 1.2.1.RELEASE. Looks like the link to the above test case is broken. Could you please re-post this link? Thanks a lot in advance!!!
        Hide
        albert kam added a comment -

        This seems to be related with This seems to be related to https://jira.springsource.org/browse/DATAMONGO-724

        Show
        albert kam added a comment - This seems to be related with This seems to be related to https://jira.springsource.org/browse/DATAMONGO-724

          People

          • Assignee:
            Oliver Gierke
            Reporter:
            albert kam
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Time Tracking

              Estimated:
              Original Estimate - Not Specified
              Not Specified
              Remaining:
              Remaining Estimate - 0d
              0d
              Logged:
              Time Spent - 34m
              34m