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

Java driver 4.2.3 Document.getList throws ClassCastException

    XMLWordPrintableJSON

Details

    • Icon: Question Question
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • None
    • None
    • None
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

    Description

      while using java driver 4.2.3 with server version - 5.0.12

      Document class getList method throws ClassCastExcepion below are the details.

      Document in DB:

      {

        “name”: “hr”,
        “id”: 1,
        “employees”: [
           {
             “name”: “paul”,
             “empId”: 1
           },
          {
            “name”: “nick”,
            “empId”: 2
          }
         ]
      }

      Code:

      public void getEmpList(){
              MongoCollection<Document> coll = mongoDatabase.getCollection("Department");
              try

      {             Document doc = coll.find().first();             List<Employee> employees = doc.getList("employees", Employee.class);         }

      catch(Exception e)

      {             e.printStackTrace();         }

      }

       

      public class Employee{

          private String name;
          private Integer empId;

           public String getName()

      {          return name;     }

          public void setName(String name)

      {        this.name = name;     }

         public Integer getEmpId()

      {        return empId;    }

          public void setEmpId(Integer empId)

      {        this.empId = empId;     }

      }

       

      Output:

      java.lang.ClassCastException: List element cannot be cast to com.Employee
      at org.bson.Document.constructValuesList(Document.java:383)
      at org.bson.Document.getList(Document.java:350)

      Attachments

        Activity

          People

            Unassigned Unassigned
            s.nivas123@gmail.com Sreenivas Gurramkonda
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: