0

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();

}

}

2 Answers 2

1

It should be

Employee emp =(Employee)itr.next();//Line 25 

you select all employees ("from Employee") and iterate over the result list that contains all your Employee entities. There's no need to cast to object[].

The for loop within the iteration loop should be obsolete as well.

EDIT: The following code should do what you intended it to do.

List l=q.list(); // you retrieve a List of Employee entities (the result of your query)
Iterator itr=l.iterator();
while(itr.hasNext())
{
    Employee emp = (Employee)itr.next();
    System.out.println(emp);   
    }
}

An alternative to the iterator may be an indexed for loop like:

for(int i = 0; i<l.size(); i++){
  Employee emp = (Employee)l.get(i);
  System.out.println(emp);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the solution but if i try to get result using my code.Is it wrong?I also want to know mistake in my code.
I edited my answer. Does this answer your question? If not: please refine your question as it is hard to understand what you really want.
0

next() in Iterator will return one of the objects from list. So, it should be like below:

Employee e =(Employee)itr.next();

if you use generic type, even you no need to cast again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.