I eagerly loading a many-to-one property in native SQL.
My current solution involves adding a
return-join element to my query declaration for the many-to-one property I am eagerly loading. A consequence of my current approach is that my result set is returned as an array. Please see below.
I don't need the result set to be returned as an array, since I can traverse to the eagerly loaded many-to-one property (result-array[1]) from the first element of the array (result-array[0]), but I do want to eagerly load the many-to-one property. Is there a way to eagerly load a many-to-one property in native SQL without including multiple
return... elements in the query declaration?
Here is the beginning of the query declaration (note the
return-join element for the many-to-one property I am eagerly loading):
Code:
<sql-query name="course.selectBySearchString_withJoin">
<return alias="course" class="Course"/>
<return-join alias="smdp_cat_code" property="course.courseCatCode"/>
select
smdp_assbch_course.rowid {course.courseId},
smdp_assbch_course.course_number {course.courseNumber},
smdp_assbch_course.title {course.title},
smdp_assbch_course.hours {course.hours},
smdp_assbch_course.cat_code_id {course.courseCatCode},
{smdp_cat_code.*}
from
smdp_assbch_course,
smdp_cat_code
where
...
Here is how I use the query:
Code:
...
// fetch courses from the database
List courses_withJoin = HibernateUtil.getSession().getNamedQuery("course.selectBySearchString_withJoin")
.setLong("school_id", school.getSchoolId().longValue())
.setString("degree_type_code", template.getDegree().getDegreeTypeCode())
.setString("search_string", searchString)
.list();
// since there is a declared join in the SQL query, Hibernate is going
// to return a list of arrays, where the course is the first element
// in each array; there has got to be a better way to do this
List courses = new ArrayList();
for(Iterator i0 = courses_withJoin.iterator(); i0.hasNext(); ) {
courses.add(((Object[])i0.next())[0]);
}
// place the courses in the request for the JSP
request.setAttribute("courseCount", new Integer(courses.size()));
request.setAttribute("courses", courses);
...
Thank you in advance for any advice on this subject.
Geoff