有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

HibernateJava。ClassCastException:java。lang.Integer不能强制转换为abc。def。我的项目。奥姆。EmployeeTopMetaData

我正在尝试使用下面代码中定义的Hibernate Criteria API运行SELECT查询。我检查了控制台,它看起来像 查询运行正常。以下是我在控制台中获得的SQL查询:

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

但就在控制台中上述SQL的下方,我看到了错误:

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)

第50行是以下方法中的下一行:

(EmployeeTopMetaData) session.createCriteria(EmployeeTopMetaData.class) 

以下方法是在EmployeeDaoImpl java类中定义的

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

这是我的实体类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;

}

共 (1) 个答案

  1. # 1 楼答案

    您的查询只返回“this.VALUE\u EMP\u ID”一个int值

    如果要返回EmployeeTopMetaData,必须更改查询:

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

    但我建议,如果您只需要VALUE\u EMP\u ID,最好只更改变量

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