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

POJO ID property is not populated after insert

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.10.0
    • Affects Version/s: 3.5.0
    • Component/s: POJO
    • None

      When inserting a new POJO into a collection, the _id is properly correctly generated in the document representation but the POJO ID property is not populated:

      MongoCollection<Document> collection;
      Document document = new Document();
      collection.insertOne(document);
      // document.get("_id") = 5a1786851ce328045c0c5276    <----- _id populated as expected after insert
      
      public class Widget {
        private ObjectId id;
      
        public String getId() {
          return id;
        }
      
        public void setId(ObjectId id) {
          this.id = id;
        }
      
      }
      
      MongoCollection<Widget> collection;  // (PojoCodecProvider configured for automatic)
      Widget widget = new Widget();
      collection.insertOne(widget);
      // widget.getId() == null    <-----   id value still null;
      
      // The ID property is properly populated when using find
      Widget widget = collection.find(...);
      widget.getId() == 5a1786851ce328045c0c5276   <--- id value set as expected
      
      

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            etay2000 Eric Tray
            Votes:
            7 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated:
              Resolved: