0

I am trying to fetch a distinct record from the database. this is my attempt

@SuppressWarnings("unchecked")
    @Override
    public List<Employee> getAllEmployees(String employeeName) { 
    String query = "SELECT DISTINCT e.name FROM Employees e WHERE e.name like '%"+ employeeName +"%'";
        List<Object[]> employeeObjects = hibernateUtil.fetchAll(query);
        List<Employee> employees = new ArrayList<Employee>();
        for(Object[] employeeObject: employeeObjects) {
            Employee employee = new Employee();
            long id = ((BigInteger) employeeObject[0]).longValue();         
            int age = (int) employeeObject[1];
            String name = (String) employeeObject[2];
            float salary = (float) employeeObject[3];
            employee.setId(id);
            employee.setName(name);
            employee.setAge(age);
            employee.setSalary(salary);
            employees.add(employee);
        }
        System.out.println(employees);
        return employees;
    }

this is the line the error occurs

for(Object[] employeeObject: employeeObjects) { //line 65

When i try to search for the record I get this error

java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
    com.ranga.dao.impl.EmployeeDAOImpl.getAllEmployees(EmployeeDAOImpl.java:65)
    com.ranga.service.impl.EmployeeServiceImpl.getAllEmployees(EmployeeServiceImpl.java:48)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:497)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    com.sun.proxy.$Proxy23.getAllEmployees(Unknown Source)
    com.ranga.controller.EmployeeController.searchEmployee(EmployeeController.java:73)

please what could be wrong?

10
  • I hope Little Bobby Tables doesn't work for you. Commented Jul 7, 2016 at 11:10
  • No idea what hibernateUtil.fetchAll does - your problem is likely there. Commented Jul 7, 2016 at 11:11
  • what is the line causing the exception? Commented Jul 7, 2016 at 11:12
  • @BoristheSpider: yes, but it's a cast to Ljava.lang.Object that causes the issue, not a cast to String Commented Jul 7, 2016 at 11:13
  • 1
    @Blaze that should not be a comment but in the question. It is extremely important to highlight the specific line the exception comes from when you post questions. Commented Jul 7, 2016 at 11:20

1 Answer 1

5

Your query is:

SELECT DISTINCT e.name FROM Employees e WHERE e.name like '%"+ employeeName +"%'

(Which is not parameterised. Bad. Very Bad. Naughty developer!)

Therefore you select unique e.name from Employees - this means you get back some names.

Therefore I would suspect hibernateUtil.fetchAll returns a List<String>.

You do:

List<Object[]> employeeObjects = hibernateUtil.fetchAll(query);

Which I suspect is an unsafe cast from List<?> or List (raw).

You then do

for(Object[] employeeObject: employeeObjects) {
...
}

At this point Java will cast the item in the List to the required type, i.e. Object[]. Hence your error.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.