nikhilgupta86
nikhilgupta86

Reputation: 492

Hibernate join query returns list of objects how should i retrieve data from object

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");

this is what i am fetching from database but it returns a list of Objects how i retrieve data from these objects..

Upvotes: 0

Views: 955

Answers (1)

JB Nizet
JB Nizet

Reputation: 692023

List<Object[]> rows = query.list();
for (Object[] row : rows) {
    Long studentId = (Long) row[0];
    String firstName = (String) row[1];
    ...
}

Note that you probably missed a huge part of the Hibernate documentation: Entities should have associations between them, and not be linked by IDs like they are given your query. For example, Student should have a (OneToMany) collection of Enrollment, and Enrollment should have (ManyToOne) field of type Course. Enrollment should not have a studentId field and a subjectId field.

Upvotes: 1

Related Questions