简体   繁体   English

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

[英]java.lang.ClassCastException: java.lang.Integer cannot be cast to java.math.BigInteger HTTP Status 500

I'm getting the following exception. 我收到以下异常。

HTTP Status 500 - Request processing failed; HTTP状态500-请求处理失败; nested exception is java.lang.ClassCastException: java.lang.Integer cannot be cast to java.math.BigInteger 嵌套异常是java.lang.ClassCastException:无法将java.lang.Integer强制转换为java.math.BigInteger

with the following code 用下面的代码

Emploee.Contoller.java 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 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? 有人知道吗?

you are executing sql statement and creating objects manually. 您正在执行sql语句并手动创建对象。 If you use HQL or criteria Hibernate does it for you, and simplifies things. 如果您使用HQL或标准,则Hibernate会为您执行此操作,并简化操作。 Use parametrized query, is a good practice, helps in preventing SQL injection 使用参数化查询是一种很好的做法,有助于防止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;
        }

When you use HQL it looks like this 当您使用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 检查这个答案

Your id is of type long , so try to cast it to Long instead of BigInteger . 您的idlong类型的,因此请尝试将其BigIntegerLong而不是BigInteger

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

Hope this helps ! 希望这可以帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.Integer不能强制转换为java.math.BigInteger - java.lang.Integer cannot be cast to java.math.BigInteger java.math.BigInteger 不能转换为 java.lang.Integer - java.math.BigInteger cannot be cast to java.lang.Integer java.lang.ClassCastException:java.math.BigInteger无法强制转换为java.lang.Long - java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long 如何解决 java.math.BigInteger 无法转换为 java.lang.Integer? - How to work around java.math.BigInteger cannot be cast to java.lang.Integer? java.lang.ClassCastException:无法将java.math.BigInteger强制转换为model.APRecord - java.lang.ClassCastException: java.math.BigInteger cannot be cast to model.APRecord 如何解决 java.math.BigInteger 无法转换为 java.lang.Integer - How to work around java.math.BigInteger cannot be cast to java.lang.Integer Playorm java.lang.Integer无法强制转换为java.math.BigInteger - Playorm java.lang.Integer cannot be cast to java.math.BigInteger java.math.BigInteger 不能强制转换为 java.lang.Integer,ZAC5C24B64B4BAC83 - java.math.BigInteger cannot be cast to java.lang.Integer , sql Dialect is not configured java.lang.ClassCastException:无法强制转换java.lang.Integer - java.lang.ClassCastException: java.lang.Integer cannot be cast java.lang.ClassCastException:无法转换为java.lang.Integer - java.lang.ClassCastException: cannot be cast to java.lang.Integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM