I am getting an error with Hibernate (I am not too familiar the API yet). Here is my code below:
public void retrieveToDB(String data){
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Query q = session.createQuery("Select empName from Employee where empName = :emp");
q.setParameter("emp", data);
List result = q.list();
Iterator iter = result.iterator();
while(iter.hasNext()){
Employee emp = (Employee) iter.next();
String e = emp.getEmpName();
System.out.println("THIS IS " +e);
}
}
I wanted to retrieve an employee name from employee table and put into a test string to be sent to the client side. But my problem is I am getting this error which I am very unfamiliar with:
Exception in thread "Thread-0" java.lang.ClassCastException: java.lang.String cannot be cast to com.datadistributor.main.Employee
I tried to find some tutorials regarding lists handling in Hibernate but I keep finding the same (unhelpful) stuff. So far nothing I’ve tried has worked out, I hope someone can help me out.
ok i updated my code and i get again new error…is there anything wrong? i hope can get some help here
I think what you’re looking to do is something closer to this:
There’s no need to store your query’s results into an enclosing
List(orArrayList) if you’re just planning to unwrap it to get at the underlyingEmployeeobject.