Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-3677

Support BsonRepresentation annotation on POJOs

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.2.0
    • Affects Version/s: None
    • Component/s: POJO
    • None
    • Needed
    • Hide

      See the changes in the commit to the .md file.

      Show
      See the changes in the commit to the .md file.

      Original Description:

      Motivation: clean POJO class do not use ObjectId in field. Pojo class do not know about mongodb logic.

      Add opportunity to serialize/deserilize document field `ObjectId _id` to `String _id` in POJO field.

      public static class TestModel {
          public String _id; // ObjectId.toHexString()
          public int id;
          public String name;
      }
      

      Now there is an exception: Failed to decode 'TestModel'. Decoding '_id' errored with: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is OBJECT_ID.

      Reproducer: http://klikr.org/869dbe58dd5df1a38b9a968c8271.txt
      Exception http://klikr.org/4386afc3820d7f89a218f98c2d79.txt

      Description after Implementation:

      To implement this, we ended up adding a BsonRepresentation annotation. This makes it easy to specify that we want to store the field as one type in the database, but convert it to a different type when used in Java POJOs. To achieve what the above example was describing, one can now do:

      public class TestModel {
          @BsonRepresentation(BsonType.ObjectId)    
          public String _id;
          public String name;    ...
      }
      

       

            Assignee:
            brian.deleonardis@mongodb.com Brian DeLeonardis (Inactive)
            Reporter:
            volyihin@gmail.com Dmitrii Volykhin
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: