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

POJO ID property is not populated after insert

    XMLWordPrintableJSON

Details

    • Icon: New Feature New Feature
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • 3.10.0
    • 3.5.0
    • POJO
    • None

    Description

      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
      
      

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: