[JAVA-5019] Java driver 4.2.3 Document.getList throws ClassCastException Created: 07/Jun/23  Updated: 27/Oct/23  Resolved: 07/Jun/23

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Major - P3
Reporter: Sreenivas Gurramkonda Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes Summary:

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)



 Comments   
Comment by Sreenivas Gurramkonda [ 07/Jun/23 ]

Thank you ross@mongodb.com for the information. I tested it and it threw exception while iteration.

Comment by Ross Lawley [ 07/Jun/23 ]

Hi s.nivas123@gmail.com,

the below code somehow works without any classcast exception:

List<Employee> employees = (List<Employee>) doc.get("employees");

can you pls give some insights on this

Correct but once you start iterating the employees list it will throw an exception - its how Java handles casting.

Comment by Sreenivas Gurramkonda [ 07/Jun/23 ]

Hi ross@mongodb.com thanks for your response. So it looks like I have to map the full Document to POJOs for it to work, but I have one question, the below code somehow works without any classcast exception

List<Employee> employees = (List<Employee>) doc.get("employees");

can you pls give some insights on this

Comment by Ross Lawley [ 07/Jun/23 ]

I'm closing this ticket as it is the expected behaviour.

Ross

Comment by Ross Lawley [ 07/Jun/23 ]

Hi s.nivas123@gmail.com,

Thank you for reaching out. The error message describes the issue, but in simple terms you are trying to cast a List<Document> into a List<Employee> hence the exception. If you want to use POJOs you should check out the POJO documentation. Your end solution should be modelled using something like: MongoCollection<Department> - where a Department Pojo represents the full document in the DB.

For future reference as this sounds like a support issue, I wanted to give you some resources to get this question answered more quickly:

  • Our MongoDB support portal, located at support.mongodb.com
  • Our MongoDB community portal, located here
  • If you are an Atlas customer, there is free support offered 24/7 in the lower right hand corner of the UI

All the best,

Ross Lawley

Comment by PM Bot [ 07/Jun/23 ]

Hi s.nivas123@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Thu Feb 08 09:03:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.