ClassCastException in iterating list returned by Query using Hibernate Query Language
Posted
by
Tushar Paliwal
on Stack Overflow
See other posts from Stack Overflow
or by Tushar Paliwal
Published on 2013-11-13T08:57:25Z
Indexed on
2013/11/13
9:54 UTC
Read the original article
Hit count: 250
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();
}
}
© Stack Overflow or respective owner