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

Custom Key with nested objects fail to find by key.

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Works as Designed
    • Affects Version/s: 3.8.2
    • Fix Version/s: None
    • Component/s: API
    • Labels:
      None
    • Environment:
      Java 8, MongoDB 4.0.6 Enterprise

      Description

      I am having problems using custom keys with nested objects. 

      If I insert a record successfully I cannot find it by its key. If I try to use the ReplaceOneModel with upsert=true, then I get BulkWriteException

      rite errors: [BulkWriteError{index=0, code=66, message='After applying the update, the (immutable) field '_id' was found to have been altered to _id:.......

      If i remove the nested object everything works fine tough.....

      This simple test fails with a complex key:

      public static class TestKeyComplex {
          private String name;
          private Integer rowNum;
          private int teszt;
          private List<String> strings;
       
       
          public TestKeyComplex(String value) {
              strings = new ArrayList<>();
              strings.add("Hello");
          }
       
          public TestKeyComplex() {
              strings = new ArrayList<>();
              strings.add("Hello");
          }
       
          public String getName() {
              return name;
          }
       
          public void setName(String name) {
              this.name = name;
          }
       
          public Integer getRowNum() {
              return rowNum;
          }
       
          public void setRowNum(Integer rowNum) {
              this.rowNum = rowNum;
          }
       
          public int getTeszt() {
              return teszt;
          }
       
          public void setTeszt(int teszt) {
              this.teszt = teszt;
          }
       
          public List<String> getStrings() {
              return strings;
          }
       
          public void setStrings(List<String> strings) {
              this.strings = strings;
          }
       
          @Override
          public boolean equals(Object o) {
              if (this == o) return true;
              if (o == null || getClass() != o.getClass()) return false;
              TestKeyComplex that = (TestKeyComplex) o;
              return teszt == that.teszt &&
                      Objects.equals(name, that.name) &&
                      Objects.equals(rowNum, that.rowNum) &&
                      Objects.equals(strings, that.strings);
          }
       
          @Override
          public int hashCode() {
              return Objects.hash(name, rowNum, teszt, strings);
          }
      }
       
      public static class TestValue {
          private String name;
          private Integer rowNum;
          private Date date;
          private EmbeddedTestClass embeddedTestClass;
       
          public TestValue() {
          }
       
          public String getName() {
              return name;
          }
       
          public void setName(String name) {
              this.name = name;
          }
       
          public Integer getRowNum() {
              return rowNum;
          }
       
          public void setRowNum(Integer rowNum) {
              this.rowNum = rowNum;
          }
       
          public Date getDate() {
              return date;
          }
       
          public void setDate(Date date) {
              this.date = date;
          }
       
          public EmbeddedTestClass getEmbeddedTestClass() {
              return embeddedTestClass;
          }
       
          public void setEmbeddedTestClass(EmbeddedTestClass embeddedTestClass) {
              this.embeddedTestClass = embeddedTestClass;
          }
       
          @Override
          public boolean equals(Object o) {
              if (this == o) return true;
              if (o == null || getClass() != o.getClass()) return false;
              TestValue testValue = (TestValue) o;
              return Objects.equals(name, testValue.name) &&
                      Objects.equals(rowNum, testValue.rowNum) &&
                      Objects.equals(date, testValue.date) &&
                      Objects.equals(embeddedTestClass, testValue.embeddedTestClass);
          }
       
          @Override
          public int hashCode() {
              return Objects.hash(name, rowNum, date, embeddedTestClass);
          }
      }
       
       
      @Test
      public void dummyDataSimpleComplexKeyInsert() {
          MongoDatabase db = mongoClient.getDatabase("TODO");
          MongoCollection mongoCollection = db.getCollection("TODO");
       
         /*
           set codec and other stuff
         */
       
          //fill object with data..
          // TODO   - MongoTestDataComplexKey -> contains key and value
          MongoTestDataComplexKey data = new MongoTestDataComplexKey(dummyKeyComplex, dummyValue);
       
          // Insert
          typedCollection.insertOne(data);
          assertEquals(1, typedCollection.find().into(new ArrayList<>()).size());
       
          // Find   
          MongoTestDataComplexKey res = typedCollection.find(eq("_id", dummyKey)).first();
          assertNotNull(res); // ------>>>> fails with null! 
          assertEquals(dummyValue, res.getValue());
       
          // Delete
          typedCollection.deleteOne(eq("_id", dummyKey));
          assertEquals(0, typedCollection.find().into(new ArrayList<>()).size());
      }
       
      
      

       

       

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: