执行Java代码遇ClassCastException:Object数组转EmployeePm失败求排查
Hey there, let's break down why this error is popping up and walk through how to fix it!
What's Causing the Exception?
Your code declares List<EmployeePm> list = empHomeSer.getEmpHome(empCode);, but the getEmpHome method is actually returning a List<Object[]> (a list of object arrays) instead of List<EmployeePm>.
This almost always happens when your persistence layer (like a DAO or repository) uses a projection query—meaning you're selecting specific fields (e.g., select e.workAssigned, e.empName from EmployeePm e...) instead of fetching full EmployeePm entities. Projection queries return arrays of objects, where each element maps to a selected field. When you try to cast one of these arrays to EmployeePm, the JVM throws that ClassCastException.
Solutions to Fix the Issue
We have two main paths depending on your original query intent:
1. If You Need Full EmployeePm Entities
If your goal was to fetch complete EmployeePm objects, update your persistence layer query to return the full entity instead of specific fields.
For example, if you're using HQL:
- Replace a projection query like
select e.workAssigned, e.empCode from EmployeePm e where e.empCode = ? - With a full entity query:
from EmployeePm e where e.empCode = ?
Once you fix the query, your original controller code will work as-is, since getEmpHome will now return a proper List<EmployeePm>.
2. If You Intended to Use a Projection Query
If you only need specific fields (so projection is intentional), adjust your code to handle the object arrays correctly:
Step 1: Update the List Type in Controller
Change the list declaration to match what the service actually returns:
List<Object[]> list = empHomeSer.getEmpHome(empCode);
Step 2: Convert Object Arrays to EmployeePm (or Extract Fields Directly)
Loop through the array list, map each array to an EmployeePm object (or just pull the fields you need). Make sure the array indices match the order of fields in your query!
Here's the updated controller code:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public String showEmpHome(ModelMap map) { System.out.println("inside showEmpHome/empHomeController"); int empCode = Integer.parseInt(htSession.getAttribute("empCode").toString()); // Use List<Object[]> instead of List<EmployeePm> List<Object[]> resultList = empHomeSer.getEmpHome(empCode); System.out.println("List size=" + resultList.size()); // Convert object arrays to EmployeePm objects List<EmployeePm> empList = new ArrayList<>(); for (Object[] objArray : resultList) { System.out.println("Raw result: " + Arrays.toString(objArray)); // Adjust indices based on your query's field order String workAssigned = objArray[1].toString(); // Example: 2nd field is workAssigned System.out.println("work = " + workAssigned); EmployeePm empPm = new EmployeePm(); // Map array elements to EmployeePm fields (adjust based on your actual query) empPm.setEmpCode((Integer) objArray[0]); empPm.setWorkAssigned(workAssigned); // Add other fields as needed from the array empList.add(empPm); } map.addAttribute("tabList", empList); System.out.println("end of showEmpHome/empHomeController"); return "empHome"; }
Step 3: Update the Service Method's Return Type
Make sure your empHomeSer.getEmpHome method is declared to return List<Object[]>:
public List<Object[]> getEmpHome(int empCode) { // Your persistence layer logic here (returns object arrays from projection query) }
Bonus Best Practice
For cleaner code and better separation of concerns, consider moving the array-to-entity conversion logic into the service layer. That way, your controller only deals with List<EmployeePm> directly, keeping it focused on handling the request and response.
内容的提问来源于stack exchange,提问作者Ayaan

