java.lang.ClassCastException: java.lang.Integer 无法转换为 abc.def.myproject.orm.EmployeeTopMetaData
java.lang.ClassCastException: java.lang.Integer cannot be cast to abc.def.myproject.orm.EmployeeTopMetaData
我正在尝试使用 Hibernate Criteria API 运行 SELECT 查询,它在下面的代码中定义。我检查了控制台,似乎是
查询 运行ning 没问题。这是我在 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)
在EmployeeDaoImpljavaclass中定义了以下方法。
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;
}
这是我的实体 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;
}
您的查询只有 returns "this_.VALUE_EMP_ID" 一个 int 值。
如果您想 return 一个 EmployeeTopMetaData,您必须更改您的查询:
Hibernate:
select
this_
from
EMPLOYEE_TOP_METADATA this_
where
this_.TESTING_ID=?
and this_.COMPANY_EMP_ID=?
但我建议,如果你只是需要VALUE_EMP_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();
我正在尝试使用 Hibernate Criteria API 运行 SELECT 查询,它在下面的代码中定义。我检查了控制台,似乎是 查询 运行ning 没问题。这是我在 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)
在EmployeeDaoImpljavaclass中定义了以下方法。
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;
}
这是我的实体 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;
}
您的查询只有 returns "this_.VALUE_EMP_ID" 一个 int 值。
如果您想 return 一个 EmployeeTopMetaData,您必须更改您的查询:
Hibernate:
select
this_
from
EMPLOYEE_TOP_METADATA this_
where
this_.TESTING_ID=?
and this_.COMPANY_EMP_ID=?
但我建议,如果你只是需要VALUE_EMP_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();