ClassCastException in iterating list returned by Query using Hibernate Query Language
- by Tushar Paliwal
I'm beginner in hibernate.I'm trying a simplest example using HQL but it generates exception at line 25 ClassCastException when i try to iterate list.When i try to cast the object returned by next() methode of iterator it generates the same problem.I could not identify the problem.Kindly give me solution of the problem.
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();
}
}