简体   繁体   中英

Hibernate retrieve multiple rows and store in an object array

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM