java.lang.ClassCastException:java.lang.Integer无法强制转换为java.math.BigInteger HTTP状态500

时间:2016-04-13 12:51:23

标签: java mysql sql hibernate

我收到以下异常。

  

HTTP状态500 - 请求处理失败;嵌套异常是   java.lang.ClassCastException:java.lang.Integer无法强制转换为   java.math.BigInteger中

使用以下代码

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;
    }

在这一行

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

有人有任何想法吗?

2 个答案:

答案 0 :(得分:1)

您正在执行sql语句并手动创建对象。如果你使用HQL或标准,Hibernate会为你做,并简化事情。 使用参数化查询,是一种很好的做法,有助于防止SQL注入

@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;
        }

使用HQL时,它看起来像这样

    @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

答案 1 :(得分:0)

您的id类型为long,因此请尝试将其投放到Long而不是BigInteger

像这样:long id = (Long) employeeObject[0].longValue();

希望这有帮助!