2017-02-20 88 views
-1

2017年2月20日上午10時04分26秒org.apache.catalina.core.StandardWrapperValve 調用重度:Servlet.service()進行的servlet [調度員]上下文 帶路徑[/ sinisukasystem]引發異常[請求處理 失敗;嵌套異常是java.lang.ClassCastException: java.lang.Integer不能轉換爲com.hendri.domain.ProductType] ,並存在根本原因java.lang.ClassCastException:java.lang.Integer不能將 轉換爲com.hendri在 com.hendri.dao.EmployeeDAOImpl.getAllEmployees(EmployeeDAOImpl.java:83) .domain.ProductType在 com.hendri.service.EmployeeServiceImpl.getAllEmployees(EmployeeServiceImpl.java:49) 在sun.reflect.NativeMethodAccessorImpl.invoke0 (本機方法)在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java.lang.ClassCastException在Spring-Hibernate項目

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

請分享Employee類,並與數據庫列的休眠映射。 –

+0

'ProductType productType =(ProductType)employeeObject [4];'您可能在此列中存儲了一個數字。 – vegemite4me

+0

你的問題是什麼? –

回答

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

你並不需要使用Object[]List。使用ListEmployee對象。

此外,有問題的查詢有Employees,但它表示List<Employee>沒有's'。我認爲這是一個錯字!

+0

employeeObject [4]是導致CCE的整數類型。不是數據被提取的方式。 hibernate映射可能存在一個缺陷導致這個問題。 –

-1

考慮到您在員工和產品之間具有多對一的關係。

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 class.Please檢查

+0

請在downvoting時說明一些解釋,這可能會幫助我改進答案。 –

相關問題