我是 hibernate 的初学者。我正在尝试使用 HQL 的最简单示例,但是当我尝试迭代列表时,它会在第 25 行 ClassCastException 生成异常。当我尝试转换迭代器的 next() 方法返回的对象时,它会生成相同的问题。我无法确定问题。请给我解决问题的方法。
Employee.java
package one;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Employee(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Employee()
{
}
}
Main2.java
package one;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Main2 {
public static void main(String[] args) {
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session s1=sf.openSession();
Query q=s1.createQuery("from Employee ");
Transaction tx=s1.beginTransaction();
List l=q.list();
Iterator itr=l.iterator();
while(itr.hasNext())
{
Object obj[]=(Object[])itr.next();//Line 25
for(Object temp:obj)
{
System.out.println(temp);
}
}
tx.commit();
s1.close();
sf.close();
}
}