嗨,我正在使用下面的代码从表 Employee 中查看 firstName 的列值,但我遇到了以下错误:
错误:
线程“main”java.lang.ClassCastException 中的异常:java.lang.String 无法在 com.servlet.prgm.ManageEmployee.listEmployees(ManageEmployee.java:90) 的 com.servlet 中转换为 com.servlet.prgm.Employee。 prgm.ManageEmployee.main(ManageEmployee.java:33)
代码 :
Query sql = session.createQuery("select firstName FROM Employee");
List employees = sql.list();
for (Iterator iterator = employees.iterator(); iterator.hasNext(); )
{
Employee employee = (Employee)iterator.next();
employee.getFirstName();
System.out.println("First Name" +employee.getFirstName());
}
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
员工类
package com.servlet.prgm;
public class Employee {
private int id;
private String firstName;
private String lastName;
private int salary;
public Employee() {}
public Employee(String fname, String lname, int salary) {
this.firstName = fname;
this.lastName = lname;
this.salary = salary;
}
public Employee(String fname){
this.firstName=fname;
}
public int getId() {
return id;
}
public void setId( int id ) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName( String first_name ) {
this.firstName = first_name;
}
public String getLastName() {
return lastName;
}
public void setLastName( String last_name ) {
this.lastName = last_name;
}
public int getSalary() {
return salary;
}
public void setSalary( int salary ) {
this.salary = salary;
}
}
抱歉,如果我的格式不正确,我是新手,我会尽快改进。
谢谢你的帮助。