Session s = ...;
String query = "from pojo.Subject sub inner join sub.students stu group by "
+ "stu.FName having count(sub.subName) > 1";
Query q = s.createQuery(query);
List<Subject> list = q.list();
for (Subject subj : list) {//error occurs here..
System.out.println(subj.getSubName()+" - "+subj.getDay());
}
It gives me an error :
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to pojo.Subject
at hibernm.Application.main(Application.java:39)
I know that I can retrieve data by :
for (int i = 0; i < list.size(); i++) {
Object get = list.get(i);
System.out.println(get);
}
But it doesn't seem like a proper way for me at all.
So I tried with introducing another
list and iterating it :
List<Subject> myList = new ArrayList<>();
for (final Object o : q.list()) {
myList.add((Subject) o);//error occurs here..
}
But it gives me the very same result.
So hope you guys get me clear. Thanks in advance.
This is because you are selecting not Subject
but inner joined
columnset, so it will not give you back List<Subject>
instead a list of column arrays including columns of Student
as well as Student
, possibly a List<Object[]>
So in order to work your query result,you might do like(or try to inspect the query result by debugging):
Query query = session.createQuery(query);
List<Object[]> listResult = query.list();
for (Object[] aRow : listResult) {
Subject subject = (Subject) aRow[0];
Student student = (Student) aRow[1];
//do your code
}