-1

Feb 20, 2017 10:04:26 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/sinisukasystem] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: java.lang.Integer cannot be cast to com.hendri.domain.ProductType] with root cause java.lang.ClassCastException: java.lang.Integer cannot be cast to com.hendri.domain.ProductType at com.hendri.dao.EmployeeDAOImpl.getAllEmployees(EmployeeDAOImpl.java:83) at com.hendri.service.EmployeeServiceImpl.getAllEmployees(EmployeeServiceImpl.java:49) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at 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;
    }
Ramanujan R
  • 1,601
  • 2
  • 23
  • 43

2 Answers2

-1

Better way,

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

You don't need to use the List of Object[]. Use List of Employee objects.

Also the query in question has Employees, but it says List<Employee> with no 's'. I think it's a typo!

Ramanujan R
  • 1,601
  • 2
  • 23
  • 43
  • employeeObject[4] is an Integer type which is causing the CCE. Not the way data is being fetched. There might be a flaw in hibernate mapping causing this issue. – VijayD Feb 20 '17 at 07:14
-1

Considering that you have the many-to-one relationship between employee and product.

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>

You might be using only productId in the hibernate file instead of Product class.Please check

VijayD
  • 826
  • 1
  • 11
  • 33
  • please state some explanations while downvoting, it might will help me to improve the answer. – VijayD Feb 20 '17 at 10:35