Search code examples
javahibernatehibernate-4.x

java.lang.ClassCastException: java.lang.Integer cannot be cast to abc.def.myproject.orm.EmployeeTopMetaData


I am trying to run a SELECT query using Hibernate Criteria API which is defined in the code below. I checked the console and it seems like the query is running fine. Here is what I am getting in the console for the SQL Query :

Hibernate: 
    select
        this_.VALUE_EMP_ID as y0_ 
    from
        EMPLOYEE_TOP_METADATA this_ 
    where
        this_.TESTING_ID=? 
        and this_.COMPANY_EMP_ID=?

But just below the above SQL in the console, I see the error :

java.lang.ClassCastException: java.lang.Integer cannot be cast to abc.def.myproject.orm.EmployeeTopMetaData 
    at abc.def.myproject.orm.dao.impl.EmpDaoImpl.insertEmployeeDetails(EmployeeDaoImpl.java:50)

And Line #50 is the following line in the below method :

(EmployeeTopMetaData) session.createCriteria(EmployeeTopMetaData.class) 

The following method is defined in EmployeeDaoImpl java class.

 public boolean insertEmployeeDetails(Employee employee)
        {
            logger.debug("Starting EmployeeDaoImpl.insert()  .....");
            Session session = null;
            Transaction tx = null;
            boolean status = true;
            try {
                session = sessionFactory.openSession();
                tx = session.beginTransaction();


               EmployeeTopMetaData empMetaData = 
                    (EmployeeTopMetaData) session.createCriteria(EmployeeTopMetaData.class) // This is the line #50
                    .setProjection(Projections.property("valueEmpId"))
                    .add(Restrictions.eq("testingId", 1234))
                    .add(Restrictions.eq("company_employee_id", 3345))
                    .uniqueResult();

                if (empMetaData == null || empMetaData. getvalueEmpId() < 1) { throw new Exception("Invalid empMetaData"); }    
                System.out.println("October 04 EmployeeTopMetaData: ");
                System.out.println(empMetaData. getvalueEmpId());


                // Some more code to go



                session.persist(employee);
                tx.commit();


            } catch(Exception ex) {
                tx.rollback();
                ex.printStackTrace();
                status = false;
            } finally {
                session.close();
            }
            logger.debug("Completed EmployeeDaoImpl.insert()  .....");
            return status;
        }

Here is my Entity Class EmployeeTopMetaData.java :

package abc.def.myproject.orm;


@Entity
@Table(name="EMPLOYEE_TOP_METADATA") 
public class EmployeeTopMetaData
{       
    public int getTestingId() {
        return testingId;
    }

    public void setTestingId(int testingId) {
        this.testingId = testingId;
    }

    public int getCompanyEmpId() {
        return company_employee_id;
    }

    public void setCompanyEmpId(int company_employee_id) {
        this.company_employee_id = company_employee_id;
    }


    public int getvalueEmpId() {
        return valueEmpId;
    }

    public void setvalueEmpId(int valueEmpId) {
        this.valueEmpId = valueEmpId;
    }

    @Id
    @Column(name="TESTING_ID")
    private int testingId;

    @Column(name="COMPANY_EMP_ID")
    private int company_employee_id;


    @Column(name="VALUE_EMP_ID")
    private int valueEmpId;

}

Solution

  • Your query only returns "this_.VALUE_EMP_ID" an int value.

    If you want to return a EmployeeTopMetaData, you have to change your query:

    Hibernate: 
        select
            this_
        from
            EMPLOYEE_TOP_METADATA this_ 
        where
            this_.TESTING_ID=? 
            and this_.COMPANY_EMP_ID=?
    

    But I suggest that if you just need VALUE_EMP_ID, it's better to change just the variable.

               Integer empMetaData = 
                    (Integer) session.createCriteria(EmployeeTopMetaData.class) // This is the line #50
                    .setProjection(Projections.property("valueEmpId"))
                    .add(Restrictions.eq("testingId", 1234))
                    .add(Restrictions.eq("company_employee_id", 3345))
                    .uniqueResult();