Query query = session.createQuery("select s.studentId as ROLL_ID, " +
"s.studentFirstName as STUDENT_NAME, " +
"s.studentDOB as DOB, " +
"su.subjectName as COURSE, " +
"e.mark as MARK " +
"from Enrolment e, Students s, Subjects su " +
"where s.studentId = e.studentId " +
"and e.subjectId = su.subjectId");
这是我从数据库中获取的内容,但它返回一个对象列表,我如何从这些对象中检索数据。
答案 0 :(得分:1)
List<Object[]> rows = query.list();
for (Object[] row : rows) {
Long studentId = (Long) row[0];
String firstName = (String) row[1];
...
}
请注意,您可能错过了Hibernate文档的大部分内容:实体应该在它们之间存在关联,而不是通过ID链接,就像给出查询一样。例如,学生应该有一个(OneToMany)注册集合,并且注册应该具有类型为Course的(ManyToOne)字段。 Enrollment
不应包含studentId
字段和subjectId
字段。