1

I am trying to retrieve multiple rows of students from the database and then store them in a list of Student Objects. My Code is as follows

Session session = this.sessionFactory.getCurrentSession();
String sql = "SELECT * FROM STUDENTS WHERE class=:clsid";
SQLQuery query= session.createSQLQuery(sql);
query.setParameter("clsid", clsid);
List<Students> stdnts= new ArrayList<Students>();
stdnts = query.list();
System.out.println("First name "+stdnts.get(0).getName());

This throws an error and did not print the value of the name variable.

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.test.stuff.model.Students

from the line

System.out.println("First name "+stdnts.get(0).getName());

How do I fix this?

1

1 Answer 1

1

Hi Monty put addEntity method before list method like stdnts = query.addEntity(Students.class).list(); then it dont throw classCast exception.

Sign up to request clarification or add additional context in comments.

1 Comment

Solved my problem. Thanks

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.