【问题标题】:java.lang.ClassCastException in Spring-Hibernate projectSpring-Hibernate 项目中的 java.lang.ClassCastException
【发布时间】:2025-12-21 20:45:11
【问题描述】:

2017 年 2 月 20 日上午 10:04:26 org.apache.catalina.core.StandardWrapperValve 在上下文中为 servlet [dispatcher] 调用 SEVERE: Servlet.service() 带路径 [/sinisukasystem] 抛出异常 [请求处理 失败的;嵌套异常是 java.lang.ClassCastException: java.lang.Integer 不能转换为 com.hendri.domain.ProductType] 根本原因 java.lang.ClassCastException: java.lang.Integer cannot 被转换为 com.hendri.domain.ProductType 在 com.hendri.dao.EmployeeDAOImpl.getAllEmployees(EmployeeDAOImpl.java:83) 在 com.hendri.service.EmployeeServiceImpl.getAllEmployees(EmployeeServiceImpl.java:49) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

@SuppressWarnings("unchecked")
    @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>();
        //List<ProductType> producttype = new ArrayList<ProductType>();
        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];
            ProductType productType = (ProductType) employeeObject[4];
            employee.setId(id);
            employee.setName(name);
            employee.setAge(age);
            employee.setSalary(salary);
            employee.setProductType(productType);
            employees.add(employee);
        }
        System.out.println(employees);enter code here
        return employees;
    }

【问题讨论】:

  • 请与数据库列共享员工类及其休眠映射。
  • ProductType productType = (ProductType) employeeObject[4];您可能在此列中存储了一个数字。
  • 你有什么问题?
  • *.com/questions/34354481/…或许能解决你的问题

标签: java spring hibernate model-view-controller


【解决方案1】:

更好的方法,

String query = "SELECT e FROM Employee e WHERE e.name like '%"+ employeeName +"%'";
//This query fetches the rows as List of Employee objects.
List<Employee> employeeObjects = HibernateUtil.getSession().createQuery(query).list(); // I'm not familiar with 'HibernateUtil'. So please confirm if this line is correct.
//Now you can get each Employee like
for(Employee employee : employeeObjects) {
        long id = employee.getId();
        int age = employee.getAge();
        String name = employee.getName();
        float salary = employee.getSalary()
        ProductType productType = employee.getProductType();
}

您不需要使用ListObject[]。使用 ListEmployee 对象。

还有问题的查询有Employees,但它说List&lt;Employee&gt;,没有's'。我认为这是一个错字!

【讨论】:

  • employeeObject[4] 是导致 CCE 的整数类型。不是获取数据的方式。休眠映射中可能存在导致此问题的缺陷。
【解决方案2】:

考虑到您在员工和产品之间存在多对一的关系。

public class Product{ 
    private long id;
    private String productName;
    public long getId() {
      return id;
    }
    public void setId(long id) {
      this.id = id;
    }
    public String getProductName() {
      return name;
    }
    public void setProductName(String productName) {
      this.productName= productName;
    }
 }
 public class Employee {
    private Long id;
    private String name;
    private Long   age;
    private Product product;
    public void setId(Long id) {
       this.id = id;   
    }
    public Long getId(){
       return this.id;
    }
    public void setAge(Long empAge) {
       this.age = empAge;
    }
    public Long getAge(){
       return this.age;
    }
    public void setName(String name){
       this.name = name;
    }
    public String getName(){
       retun this.name;
    }
    public Product getProduct(){
       retrun this.product;
    }
    public void setProduct(Product product){
       this.product = product
    }
 }

<hibernate-mapping package="com.code.java">
<class name="Product" table="product">
    <id name="id" column="product_id">
        <generator class="native"/>
    </id>
    <property name="productName" column="product_name" />
</class> 
</hibernate-mapping>

<hibernate-mapping package="net.code.java">
<class name="Employee" table="employee">
    <id name="id" column="employee_id">
        <generator class="native" />
    </id>
    <property name="name" type="string" column="name" />
    <property name="age" type="Long" column="age" />
    <many-to-one name="Product" class="com.code.java.Product"
        column="product_id" unique="true" not-null="true"
        cascade="all" />
</class>
</hibernate-mapping>

您可能在休眠文件中仅使用 productId 而不是 Product 类。请检查

【讨论】:

  • 请在投票时说明一些解释,这可能会帮助我改进答案。
最近更新 更多