0

I'm getting the following exception.

HTTP Status 500 - Request processing failed; nested exception is java.lang.ClassCastException: java.lang.Integer cannot be cast to java.math.BigInteger

with the following code

Emploee.Contoller.java

@RequestMapping("searchEmployee")
    public ModelAndView searchEmployee(@RequestParam("searchName") String searchName) {  
        logger.info("Searching the Employee. Employee Names: "+searchName);
        List<Employee> employeeList = employeeService.getAllEmployees(searchName);
        return new ModelAndView("employeeList", "employeeList", employeeList);      
    }

EmployeeDAOImpl.java

@Override
    public List<Employee> getAllEmployees(String employeeName) { 
        String query = "SELECT e.* 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();
            String name = (String) employeeObject[1];
            int age = (int) employeeObject[2];
            int admin = (int) employeeObject[3];
            boolean isAdmin=false;
            if(admin==1)
            isAdmin=true;
            Date createdDate = (Date) employeeObject[4];
            employee.setId(id);
            employee.setName(name);
            employee.setAge(age);
            employee.setAdmin(isAdmin);
            employee.setCreatedDate(createdDate);
            employees.add(employee);
        }
        System.out.println(employees);
        return employees;
    }

at this line

long id = ((BigInteger) employeeObject[0]).longValue();

Does anybody have any idea?

5
  • BigInt and Long are not same. Try-> long id = ((Long) employeeObject[0]).longValue(); Check this page: tutorialspoint.com/java/lang/long_longvalue.htm Commented Apr 13, 2016 at 12:56
  • Can you try this. long id = Long.parseLong(String.valueOf(employeeObject[0])) ? this should work, Commented Apr 13, 2016 at 13:01
  • changed,interesting,now i got new exception is java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Integer Commented Apr 13, 2016 at 13:05
  • Deendayal Garg,long id = Long.parseLong(String.valueOf(employeeObject[0])) it helped, you can duplicate the answer, so that I can choose it as a right. Commented Apr 13, 2016 at 13:14
  • 1
    Why aren't you using HQL ? Commented Apr 13, 2016 at 13:19

2 Answers 2

1

you are executing sql statement and creating objects manually. If you use HQL or criteria Hibernate does it for you, and simplifies things. Use parametrized query, is a good practice, helps in preventing SQL injection

@Override
        public List<Employee> getAllEmployees(String employeeName) { 
            String query = "SELECT e.* 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();
                String name = (String) employeeObject[1];
                int age = (int) employeeObject[2];
                int admin = (int) employeeObject[3];
                boolean isAdmin=false;
                if(admin==1)
                isAdmin=true;
                Date createdDate = (Date) employeeObject[4];
                employee.setId(id);
                employee.setName(name);
                employee.setAge(age);
                employee.setAdmin(isAdmin);
                employee.setCreatedDate(createdDate);
                employees.add(employee);
            }
            System.out.println(employees);
            return employees;
        }

When you use HQL it looks like this

    @Override
        public List<Employee> getAllEmployees(String employeeName) { 
        Session session = //initialize session            
        Query query = session.createQuery("FROM Employees e WHERE e.name like '%"+ ? + "%'");
           query.setParameter(0, "%"+employeeName+"%");
           List<Employee>  employees = query.list();
           System.out.println(employees);
           return employees;
        }

Check This Ans

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

Comments

0

Your id is of type long, so try to cast it to Long instead of BigInteger.

Like this: long id = (Long) employeeObject[0].longValue();

Hope this helps !

1 Comment

long id = (Long) employeeObject[0].longValue(); incorrect line, and i am repeating that me helped this long id = Long.parseLong(String.valueOf(employeeObject[0]))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.