我有两个由外键关联的测试表
- 表
School
(打开 fkstudent_id
) - 表
Students
(带有 pkid
) - FK:
school.student_id -> students.id
如何使用此查询获取学生姓名?
Session session = DaoSF.getSessionFactory().openSession();
String query = "";
Query criteria;
query = "from School s " +
"inner join s.student st " +
"where s.student_id = st.id";
criteria = session.createQuery(query);
当我尝试迭代数据时:
for(Iterator<?> iter = criteria.iterate(); iter.hasNext();)
{
Object item = (Object)iter.next();
System.out.println(((School) item).getStudent().getId());
}
它报告:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to table.School
但映射工作正常,因为当我将最后一行更改为
System.out.println(item.toString());
我有所有的数据,但像这样:
[Ljava.lang.Object;@1d2300e
[Ljava.lang.Object;@51207c
[Ljava.lang.Object;@2bd615
...