我收到以下异常。


  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();


有人知道吗?

最佳答案

您正在执行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

09-25 21:46